Jump to content

PHP - Displaying Images From Folder, With Most Recent First


Steve1

Recommended Posts

The problem:

 

I'm trying to create a page which outputs images from a folder which I have been

able to do, but the problem I'm having is not being able to get the page to display

the most recent image according to file modification date at the top.

 

The first set of code below outputs the image timestamps in descending order, from newest to oldest which is

what I want, but as soon as I change/add a couple lines of code (Shown in the second lot of code) to get the image

file name along with the timestamp, the echoed list (timestamp and file names) gets muddled up in a random order.

 

In short;

As soon as the file names are retrieved with the timestamp, the list

goes from being organised descendingly, to not.

 

Show timestamp only code (1st lot of code):

 

<?php

//Open images directory
$ignore = array("..",".");

$dir = opendir("images1");

$images = array();

$sortedimages = array();

//List files in images directory
while (($file = readdir($dir)) !== false) 

if (!in_array($file, $ignore))	

$images[] = $file;

foreach ($images as $image)

{

$filetime = filemtime("images1/$image");

$sortedimages[] = $filetime;

}

rsort($sortedimages);

foreach ($sortedimages as $sorted)

{

//foreach ($sorted as $key => $value)

//{

echo "$sorted<br/>";

}

//}

closedir($dir);

?>

 

The show timestamp and file name code (2nd lot of code):

 

<?php

//Open images directory
$ignore = array("..",".");

$dir = opendir("images1");

$images = array();

$sortedimages = array();

//List files in images directory
while (($file = readdir($dir)) !== false) 

if (!in_array($file, $ignore))	

$images[] = $file;

foreach ($images as $image)

{

$filetime = filemtime("images1/$image");

$sortedimages[] = array($filetime => $image);

}

rsort($sortedimages);

foreach ($sortedimages as $sorted)

{

foreach ($sorted as $key => $value)

{

echo "$key and $value<br/>";

}

}

closedir($dir);

?>

 

The changes made in the 2nd script from the 1st:

 

//Changed:
$sortedimages[] = $filetime; ---> $sortedimages[] = array($filetime => $image);

//Included the previously commented out:
foreach ($sorted as $key => $value)
{

}

//Changed:
echo "$sorted<br/>"; ---> echo "$key and $value<br/>";

 

Thanks for any help!

I'm still looking thru your code to see if I can find answers to your specific questions.  But in the meantime, I did notice a few things to comment on.

 

1) Is this code copy/pasted directly from your php file?  If so, I don't see how it could actually be working.  I don't see any curly braces following the if statement or the while statement.

//List files in images directory
while (($file = readdir($dir)) !== false) 

if (!in_array($file, $ignore))

 

2) Rather than testing the current "file" to see if it's the current or parent folder, you could use the is_file function.  Besides even if it's not the current or parent folder, that doesn't mean it's not a child folder. 

 

http://us.php.net/manual/en/function.is-file.php

Try this on for size.  This goes AFTER the while loop that creates the $images array (list of image filenames).

 

<?php
foreach ($images as $image)
{
$filetime = filemtime("images1/$image");
$sortedimages[]$filetime => $image;
}
krsort($sortedimages);

echo "<pre>";
print_r($sortedimages);
echo "</pre>"
?>

 

Edit:  the biggest change I made was to use krsort which is described at the following URL:

 

http://us.php.net/manual/en/function.krsort.php

Thanks for the help.

 

If I enter the code that was provided, this error appears:

Fatal error: Cannot use [] for reading "My/Directory/Here" on line 98.

Line 98 is:

$sortedimages[]$filetime => $image;

I had a play about with it, but I just can't figure out what to do.

 

Also, I did copy and paste the code above straight from my PHP file.

Cheers

Thanks for the help, it's working now!

 

I changed the first suggested bit of code to stop at krsort, as well as changing the $sortedimages as suggested.

I then modified my existing code so I could get the timestamp and filename of the images

into separate variables and it's now outputting exactly as I wanted, in descending order with newest image uploaded at the top: Such as;

 

1290370828 and House MD 1.jpg

1290370799 and Hugh Laurie - House.jpg

1290346142 and house.jpg

 

Thanks.

  • 2 months later...

Can u please post the ending script i've been trying to do the same thing for some images of mine..

<?php
$dir = "./plate_cache";
$handle = opendir($dir);
while (($file = readdir($handle))!==false)
{
if (strpos($file, '.png',1)) 
	{
	echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a>   ";
	}
}
closedir($handle); 
}

?>

 

Can u please post the ending script i've been trying to do the same thing for some images of mine..

<?php
$dir = "./plate_cache";
$handle = opendir($dir);
while (($file = readdir($handle))!==false)
{
if (strpos($file, '.png',1)) 
	{
	echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a>   ";
	}
}
closedir($handle); 
}

?>

 

Andrew,

 

Give us some info. 

 

[*]What is it you're trying to accomplish?

[*]What is it doing that you don't want it to do?  Or what is it NOT doing that you want it to do?

[*]Are there any error messages?

 

EDIT:  You'd be better off starting a new thread to ask for help even if you want to reference this thread.  This thread is marked as "Solved" so people who can help may not read this thread.  The only reason I opened it is that I received an email messages from the system telling me that someone had replied to this thread.

From a directory "/plates-cache" display all .png images in the directory in descending order by their filetime

 

1. I am trying to display images on a html page

2. Display them in descending order by their filetime

 

the script works fine for part 1 I have no idea how to do part 2

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.