Jump to content

Recommended Posts

Hello.  My website has a photo gallery of thumbnails that is created by reading all photo files in a specified directory.

Here is my function that builds the array which is ultimately displayed in the gallery...

	<?php
    function getPhotoFilesArray($photoPath){
        /**
         * Takes path to photo-directory, and returns an array containing photo-filenames.
         */
	    // Initialize Array.
    $photoFiles = array();
	    // Check for Photo-Directory.
    if (is_dir($photoPath)){
      // Photo-Directory Found.
      // Open Directory-Handle.
      $handle = opendir($photoPath);
	      // Open Photo-Directory.
      if($handle){
        // Initialize Key.
        $i = 1001;
	        // Iterate through Photo-Directory items.
        while(($file = readdir($handle)) !== FALSE){
          // Return next Filename in Directory.
          
          // Define fullpath to file/folder.
          $fullPath = $photoPath . $file;
	          // Populate Array.
	          if(!is_dir($fullPath)
              && preg_match("#^[^\.].*$#", $file)){
            // Not Directory.
            // Not Hidden File.
            // Add to array.
            $photoFiles[$i] = $file;
	            $i++;
          }//End of POPULATE ARRAY.
          
        }//End of ITERATE THROUGH PHOTO-DIRECTORY ITEMS
	        closedir($handle);
        
      }//End of OPEN PHOTO-DIRECTORY
	    }else{
      // Photo-Directory Not Found.
      // Redirect to Page-Not-Found.
      header("Location: " . BASE_URL . "/utilities/page-not-found");
	      // End script.
      exit();
	    }//End of CHECK FOR PHOTO-DIRECTORY
	        return $photoFiles;
	    }//End of getPhotoFilesArray
	?>
	

 

Everything works fine locally in DEV, but when I uploaded my website (and photos) onto a webserver, the photos are appearing in a backwards order in PROD.

This is annoying, because I want the photos displayed chronologically from oldest (first) to newest (last).

I'm not sure where the problem is happening, because each photo was taken with my camera and by nature of the camera, photo names are incremented by one, so IMG_001.jpg would have been taken FIRST, followed by IMG_002.jpg, IMG_003.jpg, and so on.

How can I fix things so the photos are displayed in the order they were physically taken AND match how things display locally in DEV?

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/309743-fixing-order-of-photos/
Share on other sites

34 minutes ago, requinix said:

Didn't we tell you to use glob()?

Yes, but I had already coded things as shown above, and it made no sense to recode things.

Either way, I don't see where the issue is....

If my photos haven't changed between local DEV and PROD - they have the same filenames and created on dates - then why would anything change?

The only thing I can think is maybe when I SFTPed the files to my webserver it changed the dates on the files and this the order the files are in on the server?

 

56 minutes ago, SaranacLake said:

Yes, but I had already coded things as shown above, and it made no sense to recode things.

Here's a really simple reason that you should be able to understand:

If your code doesn't do what you want it do then you need to recode things.

We're talking about removing a couple lines and changing a couple lines. It's not rocket surgery.

56 minutes ago, SaranacLake said:

Either way, I don't see where the issue is....

Read the other thread where we told you why you should use glob() until you figure out why.

56 minutes ago, SaranacLake said:

If my photos haven't changed between local DEV and PROD - they have the same filenames and created on dates - then why would anything change?

Because you don't understand what the problem is.

56 minutes ago, SaranacLake said:

The only thing I can think is maybe when I SFTPed the files to my webserver it changed the dates on the files and this the order the files are in on the server?

No.

4 hours ago, requinix said:

Here's a really simple reason that you should be able to understand:

If your code doesn't do what you want it do then you need to recode things.

We're talking about removing a couple lines and changing a couple lines. It's not rocket surgery.

Read the other thread where we told you why you should use glob() until you figure out why.

Because you don't understand what the problem is.

No.

I added this one line to my function and it appears to have fixed the issue...

	asort($photoFiles);
	

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.