Jump to content
#FlattenTheCurve ×
SaranacLake

Gallery photos in wrong order

Recommended Posts

I have a php page that creates a photo gallery with thumbnails.  It is populated by code that reads all photo files from a specified photo directory.

This was working fine in DEV, but now that I have uploaded to my test web server, the pictures are in reverse order.

Not the end of the world, yet annoying, because they should be in chronological order.

Files names are straight off my iPhone (e.g. IMG_2203.jpg, IMG_2204.jpg, IMG_2207.jpg)

What is happening, and how can I fix this?

Thanks!

Share this post


Link to post
Share on other sites

So you're getting the file names, getting the creation dates for those files, storing in an array then sorting the array by date - yes?

Share this post


Link to post
Share on other sites
4 hours ago, Barand said:

So you're getting the file names, getting the creation dates for those files, storing in an array then sorting the array by date - yes?

I call a function (below) that reads the files in the photo directory and puts them into an array...

	function getPhotoFilesArray($photoPath){
	    $photoFiles = array();
	    // Check for Photo Directory.
    if (is_dir($photoPath)){
      // Photo-Directory Found.
      // Open Directory-Handle.
      $handle = opendir($photoPath);
	      if($handle){
        // Initialize Key.
        $i = 1001;
        
        // Iterate through Photo-Directory items.
        // Return next Filename in Directory.
        while(($file = readdir($handle)) !== FALSE){
          // File or Directory exists.
          if($file != '.' 
              && $file != '..' 
              && $file != '_thumbnails'
              && $file != '_photos'
              && preg_match("#^[^\.].*$#", $file)){
            // Not Directory.
            // Not Hidden File.
            // Add to array.
            $photoFiles[$i] = $file;                     // Simple array.
	            // Increment Key.
            $i++;
          }
        }
        closedir($handle);
      }
	    }else{
      // Photo-Directory Not Found.
      // Redirect to Page-Not-Found.
      header("Location: " . BASE_URL . "/utilities/page-not-found");
	      // End script.
      exit();
	    }//End of RETRIEVE GALLERY-IMAGES
	        return $photoFiles;
	    }//End of getPhotoFilesArray
	

 

Then in the HTML section, I iterate through the array using a FOR-EACH loop.

Locally in my DEV environment this works fine, but on the web server something is getting messed up.

 

Share this post


Link to post
Share on other sites

There is nothing in that code to sort the results in the array. readdir() (as stated in the manual) returns the results " . . . in the order in which they are stored by the filesystem". You could determine the file date and sort by that, but  since you are copying them from one device to another, there is a good chance that the file date will be changed in that process. Sometimes the date changes in a copy process and sometimes it does not - depends on the process used. Also complicating things, filetime() will return different values based on the filesystem. As I understand it, in Windows it returns the creation time, whereas in Unix it returns the modification time. So, you could potentially sort using filetime() IF the original file creation times are preserved after they are copied AND filetime() returns the creation date. You should be able to verify if those are both true in your case. If so, get the creation date using filetime() and sort on that. If that is NOT the case, then you could either ensure they are copied in the correct order (i.e. one at a time in the order they were created) or you need to sort using some manner. For example, the named of the files is sequential. So you could sort by file name. But, when you go from IMG_9999.jpg to IMG_10000.jpg it will break that logic as IMG_1... comes before IMG_9... when sorting alpha-numerically. Of course, you could always parse the file names and convert them to have x number of digits, i.e. IMG_9999.jpg converts to IMG_0009999.jpg and IMG_10000.jpg converts to IMG_0010000.jpg. Also, your phone *may* restart that numbering at some point which basically screws all of that up.

So, lots of possibilities, but no bullet-proof solution. The *best* solution would be predicated on the questions on how the files are handled in the environment (dates when copied, whether file names change, etc.).

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.