1

I have a couple images in my images folder for example:

User12345_gallery0.png
User12345_profilePic.jpg
User12345_gallery1.png
User12345_gallery2.jpg
User54321_gallery0.png

What I'm trying to do is pass the user to my getimages.php and get that user's images. I've never done this before so I'm struggling with the syntax. Here's what I have so far:

if($_REQUEST['user']){
$ext = array('jpeg', 'png', 'jpg');
$dir = 'images/';
$user = $_REQUEST['user'];
$images = array();
foreach ($ext as $ex){
    if (file_exists(strpos($dir.$user."_gallery", "gallery"))) {
        //add file to $images???
    }
}
}

I want to check if the file contains the username and the instance of "gallery". How can I do that? Am I on the right track?

4
  • To begin with, file_exists() expects a file name and strpos() returns a string offset. Commented Apr 21, 2014 at 16:38
  • does your user id always have the same number of characters? Commented Apr 21, 2014 at 16:39
  • You could use glob to do a pattern search. However, since it needs to scan all the filenames in the entire directory on each call, it will perform horribly if your site gets popular. Commented Apr 21, 2014 at 16:39
  • Yes, I understand that is wrong. I don't know the exact file name...and I just noticed that I'm not looping through the entire images folder... Commented Apr 21, 2014 at 16:39

2 Answers 2

3

Use glob() and stripos() for this..

<?php

if($_REQUEST['user']){

    $user = $_REQUEST['user'];
    $images = array();
     foreach (glob("images/*.{jpg,png,gif}", GLOB_BRACE) as $filename) {
         if(stripos($filename,$user)!==false && stripos($filename,'gallery')!==false)
            $images[]=$filename;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

shouldn't the OP use glob(array('jpeg', 'png', 'jpg'))...?
Why not just use a pattern with glob? glob($user."_gallery*")
@JonathanKuhn looks like "_gallery" is not always part of the pattern, there is "profilePic" for example...
You can't append an array to a string like that. You could use glob($user."_gallery*.{jpg,png,gif}", GLOB_BRACE);
Sorry to turn your comments into a conversation :). I would just suggest that you use the $user."_gallery*.{jpg, png, gif}" pattern if searching for a specific user that you know idenified in $user and using "*_gallery*.{jpg, png, gif}" if searching for gallery for all users.
|
1

Why not just look at the pictures names in the directory and return the ones for your user ?

$ext = array('jpeg', 'png', 'jpg');
$dir = 'images/';
$user = $_REQUEST['user'];

// the length of your username (for the expression bellow)
$usernamelength  = strlen($user); 

// read a list of all files into an array
$filesindirarray = scandir($dir);

// loop through the list
foreach($filesindirarray as $filename)
{
   // does the filename start with
   if(substr($filename,0,($usernamelength+8)) ==  $user .'_gallery')
   {

      // fish out the files extension
      $fileext = pathinfo($filename, PATHINFO_EXTENSION);

      // if the extension of the files is in your list
      if(in_array($fileext,$ext))
         // add to images
    }
} 

2 Comments

Thanks for trying to help but in my question I showed that the way the images are named, some of them have "user_gallery..." and some have "user_profilePic...". So I don't think your way would get back just the gallery pics
I changed the code sothat only pictures with _aggery in the name are returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.