• After witnessing the continued decrease of involvement in the SpotterNetwork staff in serving SN members with troubleshooting issues recently, I have unilaterally decided to terminate the relationship between SpotterNetwork's support and Stormtrack. I have witnessed multiple users unable to receive support weeks after initiating help threads on the forum. I find this lack of response from SpotterNetwork officials disappointing and a failure to hold up their end of the agreement that was made years ago, before I took over management of this site. In my opinion, having Stormtrack users sit and wait for so long to receive help on SpotterNetwork issues on the Stormtrack forums reflects poorly not only on SpotterNetwork, but on Stormtrack and (by association) me as well. Since the issue has not been satisfactorily addressed, I no longer wish for the Stormtrack forum to be associated with SpotterNetwork.

    I apologize to those who continue to have issues with the service and continue to see their issues left unaddressed. Please understand that the connection between ST and SN was put in place long before I had any say over it. But now that I am the "captain of this ship," it is within my right (nay, duty) to make adjustments as I see necessary. Ending this relationship is such an adjustment.

    For those who continue to need help, I recommend navigating a web browswer to SpotterNetwork's About page, and seeking the individuals listed on that page for all further inquiries about SpotterNetwork.

    From this moment forward, the SpotterNetwork sub-forum has been hidden/deleted and there will be no assurance that any SpotterNetwork issues brought up in any of Stormtrack's other sub-forums will be addressed. Do not rely on Stormtrack for help with SpotterNetwork issues.

    Sincerely, Jeff D.

PHP Script to Display Latest Image in Directory

Joined
Mar 2, 2004
Messages
2,404
Location
Northern Colorado
Hey all,

I'm coming short on my searches for a code and I was hoping to find some help here..

I've recently ventured into PHP for the first time during the updating of my site, and one thing I wanted to do was have a section that displays the latest image uploaded to a specific directory. Namely the directory to where my cell phone-sent photos go.

I've found a few close calls, but nothing which I would think would be easy.

Basically all I am trying to do is display an image on my website, but it is the latest uploaded image from a specific directory. When a new image is uploaded, the image on the page will change to that new image.

I'm fiddling with the filemtime and filectime commands, but still am coming up short.

Code:
<?php
$ctime = 0;
foreach (glob('*.jpg') as $file)
{
if ($mtime > filemtime($file))
{
$match = $file;
$mtime = filemtime($file);
}
}

print '<img src="' . htmlentities($file) . '" alt="Image Title Here">';
?>

That doesn't seem to want to post the most recently uploaded picture.

Any help would be appreciated!
 
I believe your > should be <

Since the latest file should have the largest time? I have only dabbled in php, but the logic seems to be backwards.
 
Tony, do the images that are uploaded to the directory have any specific date/time naming convention? If so, code can be written using the sort() command to sort an array of filenames by name/number, then finding the last (or first) index of the sorted array using end() which would identify the oldest/newest file, of course if the default file naming convention uses some sort of time/date or incremental system. Once that file is found, simply copy it to a static file name like "latestWhatever.jpg". This is how I go about displaying the latest StormLab4 image on my "Chase Mode" page on Underthemeso.com I'm not sure how cell phones photos are assigned filenames since I don't have a camera cell phone.
 
I believe your > should be <

Since the latest file should have the largest time? I have only dabbled in php, but the logic seems to be backwards.

Isn't programming fun.. stupid little things... I'll give that a shot and see (which may be exactly why the image never changed)! Thanks for catching that, Scott!

And Mike, I don't think there is any real pattern to the image naming. I do see your idea and may ask to see that particular piece of code in the event I re-impliment something similar with GRLevel3. The StormLab software can be configured to upload over images, and since I am not archiving (at the moment), I am sufficed with that. But, archiving may prove to be useful in the future, but I can do most of that on my own PC.

Thanks again!
 
Here's what you want:

Code:
$dir = '/home/tony/file_dir';
$base_url = 'http://www.website.com/file_dir';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
 while (false !== ($file = readdir($handle))) {
    if (($file != '.') && ($file != '..')) {
       $mtime = filemtime("$dir/$file");
       if ($mtime > $newest_mtime) {
          $newest_mtime = $mtime;
          $show_file = "$base_url/$file";
       }
    }
  }
}
print $show_file;
 
I still feel like a PHP newbie, so don't take anything I say without checking it, but (assuming the rest of Tyler's script does what you want), I think Tony wants the print statement to be:
Code:
print '<img src="' . $img_folder.$image) . '" alt="Image Title Here">';

Tony,
It sounds iike what you want is similar to an RSS feed for a directory. When something new is added, you want it to show up automatically somewhere. That being the case, you might find these PHP functions useful to your endeavors: RSS Feeds for FTP Servers

I also found a Google cache of a page that tells you how to get the 5 newest files in a directory:
http://74.125.45.104/search?q=cache...ectory&hl=en&ct=clnk&cd=3&gl=us&client=safari. I think you would have to modify the instructions to do something similar to what Tyler did above (start with a defined base URL variable and then append the resulting filename and put that in a IMG tag.)

In broader terms, what you are describing involves File and Directory Manipulation in PHP, so this tutorial may be useful.

Hope this is somewhat helpful.
 
Last edited by a moderator:
Actually...

Code:
print '<img src="'.$show_file.'" alt="Image Title Here">';
but you had the jist.

The google cache link you provide is pretty good, but using a class for this is way overkill. I use something similar to the last example at AllisonHouse for one of my feeds, but in this case throwing all files into an array and then doing a sort and taking the last one is unnecessary and potentially a memory hog (think about 100,000 images in that directory).
 
Thanks for the input guys...

This is the code I have in place for this page...

Code:
<?php
$dir = 'fileDir/';
$base_url = 'http://www.tornadoeskick.com/fileDir/';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
 while (false !== ($file = readdir($handle))) {
    if (($file != '.') && ($file != '..')) {
       $mtime = filemtime("$dir/$file");
       if ($mtime > $newest_mtime) {
          $newest_mtime = $mtime;
          $show_file = "$base_url/$file";
       }
    }
  }
}
print '<img src="' .$show_file. '" alt="Image Title Here">';
?>

It is working now! Thank you so much for your time with this!!!!

Now I have to get the mobile images set back up and she'll be good to go!

Again, thanks for the help on this!

Last question as I realized this to be a problem... that code will print out whatever the file, regardless of extension; using the original code's "for each" line, can I tell this to only do it with a certain extension (i.e. JPEGs)?
 
I believe that first you would need a function like this:
Code:
/**
* gets the filetype for an image
*
* @param  string  $file the filename
*/
function FiletypeFromName($file)
{
	preg_match("/\.(\w*)$/", $file, $matches);

	return isset($matches[1]) ? $matches[1] : false;
} // end FiletypeFromName();

Then you would use that function to get the file type from your files, like:
Code:
$filetype = FiletypeFromName($file);
And once you have that you can create an IF statement like:
Code:
if ($filetype == "jpg" || $filetype == "jpeg")

I'd probably incorporate that last part in with your current "if ($mtime > $newest_mtime) line:
Code:
if (($mtime > $newest_mtime) && ($filetype == "jpg" || $filetype == "jpeg"))

Again, I'm a newbie, so more experienced programming eyes will be better at checking my solution.
 
Back
Top