Jump to content

Display last 3 image files uploaded to a folder


WOWDesigns

Recommended Posts

Hi

 

Basically I've built a CMS where by my clients can upload a number of images. On the success page I want to display the images they uploaded by file name.

 

The issue is the number of images can vary. They may upload 2 or 10 or 50 etc.

 

So far I've come up with this:

 

// number of files
$UN = 3; //I've set this to 3 for now, but this is passed from the upload page!

// server directories and directory names
$dir = '../properties';

$images = glob($dir.'/*.{jpg}', GLOB_BRACE); //formats to look for

$num_of_files = $UN; //number of images to display from number of uploaded files

foreach($images as $image)
{
$num_of_files--;

$newest_mtime = 0;
$image = 'BROKEN';
if ($handle = @opendir($dir)) {
 while (false !== ($file = readdir($handle))) {
		if (($file != '.') && ($file != '..')) {
			 $mtime = filemtime("$dir/$file");
			 if ($mtime > $newest_mtime) {
					$newest_mtime = $mtime;
					$image = "$file";
			 }
		}
	}
}

if($num_of_files > -1) //this made me laugh when I wrote it
echo $trimmed = ltrim($image, "../properties").'<br />'; //display images
else
break;
}

 

Without this piece of code:

 

	$newest_mtime = 0;
$image = 'BROKEN';
if ($handle = @opendir($dir)) {
 while (false !== ($file = readdir($handle))) {
		if (($file != '.') && ($file != '..')) {
			 $mtime = filemtime("$dir/$file");
			 if ($mtime > $newest_mtime) {
					$newest_mtime = $mtime;
					$image = "$file";
			 }
		}
	}
}

 

It shows the first 3 files alphabetically. I want to view the last number of images added.

 

With the above code it simply shows the last image added 3 times!

 

So I need to get the time each image was added and then order by the newest added and limit to the number of images uploaded. Any suggestions please?

 

Kindest regards

Glynn

Link to comment
Share on other sites

Hi, thanks for the reply.

 

To me they are the same thing in essence, but I guess there could be different code that you could use when uploading or simply checking the folder.

 

To be concise, the user chooses how many images they want to upload. On completion they are passed to a success page which will inform them the name of the files uploaded.

 

Previously I just passed the number of files they uploaded and said "You have successfully uploaded X files to the folder foldername". Now I want to be more specific and say "You have successfully uploaded the following files to the folder foldername: *file list*".

 

Kindest regards

Glynn

Link to comment
Share on other sites

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.