show latest image form directory.

Hi, Im trying to have a ‘latest image’ displayed on my gallery page, Im using the code below but havnt been able to get it working. What am I doing wrong?



The code seem to be fine. Do you mean it is displaying wrong file (with not most recent modification time)? Or not displaying file at all?

not displaying at all, perhaps ive implemented it wrong as ive simply dropped it into my HTML. if u could follow the url to the page u will see.

Oh, I didn’t notice - what this code mean??

<script type="php">

Probably you need to give your file a .php extension, instead of .html (unless you have set mime type for your gallery.html).

Ok great, Ive renamed to .php extension and the code is running now, but with error:

Warning: opendir(IDM-Photos/) [function.opendir]: failed to open dir: No such file or directory in /home/ttest/public_html/sa/gallery.php on line 59

Note ive ammended the directory pointer to this:

[php]$dir = ‘IDM-Photos/’;
$base_url = ‘’;[/php]

as this is where the images are uploaded to.

So at this stage ive googled the error and have made sure the folder allows directory indexing. Not sure what to do next.


You need to log in to your server and check if there is directory named ‘IDM-Photos’ in this location:


note, on Unix/Linux this is case sensitive.

Oh ok its actually at

or would it be


After some fiddling around Ive got it to the stage where its dealing with an image in the correct directory.

The error states:

[b]Warning: filemtime() [function.filemtime]: stat failed for home/ttest/public_html/sa/.htaccess in /home/ttest/public_html/sa/gallery.php on line 62

Warning: filemtime() [function.filemtime]: stat failed for home/ttest/public_html/sa/Koala.jpg in /home/ttest/public_html/sa/gallery.php on line 62[/b]

so the second error deals with koala.jpg which is the image in the folder.
of course this is still not working the way it is suppose too!

but what to do now?

You may have permission issue reading these files. Make sure files is readable for user, apache is running under. Also skip checking file .htaccess:
[php]if (($file != ‘.’) && ($file != ‘…’) && ($file != ‘.htaccess’)) {[/php]

Thanks for all your help phphelp
Ive now got this working nicely.
1 final question how much more would it take to have this search all subsequent directories under “albums” as such it searches one directory “IDM-Photos” but if there were multiple directories could it search all of them?

To do this would be the icing on the cake.

anyway the working code ended up like this:

$dir = ‘home/ttest/public_html/sa’;
$base_url = ‘gallery3/var/albums/IDM-Photos’;
$newest_mtime = 0;
$show_file = ‘BROKEN’;
if ($handle = opendir($base_url)) {
while (false !== ($file = readdir($handle))) {
if (($file != ‘.’) && ($file != ‘…’) && ($file != ‘.htaccess’)) {
$mtime = filemtime("$base_url/$file");
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = “$base_url/$file”;
print ‘Image Title Here’;

thanks again phphelp

You’re welcome!

Here is the function that may help you. This function recursively searches files in the given directory ($mydir), and all the subdirectories, and generates an array of all files found ($results):

[php] function find_files($mydir){

global $results;
$fileslimit = 1000;  // max number of files to return

if(([email protected]($mydir))!==false){
    if($f!="." and $f!=".." and $f!=".htaccess"){
        if(find_files($mydir."/".$f)>=$fileslimit) return count($results);
        if(count($results)>=$fileslimit) return count($results);

return count($results);


You can use the function above as shown below, or just modify it as you need (so that it only return newest file, and not an array of all files found in directory/subdirectories):
$base_url = ‘gallery3/var/albums/IDM-Photos’;
$newest_mtime = 0;
$show_file = ‘BROKEN’;

$results = array();

if(find_files($base_url)){ // if some files found, check modification time for each file
foreach($results as $file){
$mtime = filemtime("$file");
if($mtime > $newest_mtime){
$newest_mtime = $mtime;
$show_file = $file;
print ‘Image Title Here’;

Ok thanks, Ive implemented that code but needed to add the directory “sa” in the print line

[php] print ‘Image Title Here’;[/php]

as the image location before this in your code was returning the directory structure with the ‘sa’ missing.
now its working very nicely

again thanks heaps, your a champ!

Sponsor our Newsletter | Privacy Policy | Terms of Service