Pull single image from folder

I would like to pull a single image out of the folder ‘upload/’ to display instead of all of them. Is this possible? It doesn’t need to be the same picture all of the time (in fact I would like it to be the most recent). Any help is always appreciated

[php] <?php

$image_dir = 'upload/';
$per_column = 1;      

$validExt = array(
    'png' => 'image/png',
    'jpeg' => 'image/jpeg',
    'jpg' => 'image/jpg',
    'gif' => 'image/gif',
	'mp3' => 'audio/mp3',
);

if ($handle = opendir($image_dir)) {
    while (false !== ($file = readdir($handle))) 
    {
        $ext = strtolower(substr($file, -3));
        if (isset($validExt[$ext])) 
        {
            $stats = stat($image_dir.$file);
            $files[$stats['mtime']] = $file;
        }
    }
    closedir($handle);
}

$count = 1;
krsort($files);

$cnt = count($files);

if($cnt)
{
    foreach($files as $file)
    {
        $count;
       echo '<a href="' . $image_dir . $file . '" rel="scripts/lightbox" title="' . $file .'" ><li style="list-style-type:none"><img src="' . $image_dir . $file . '" width="300" height="225" title="" /></li></a><caption style="text-align:center"><i>Image pulled from<a href="Upload_Page.php">UPLOAD</a>&nbsp;page</i></caption>'.chr(10);
        if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
    }
}
else
{
    echo 'no pictures yet...';
}
?>[/php]

When do you want to pull it? you can, but you’d have to know the name of it or have all the pics in an array (but even then, you’d have to know the name of it).

I want to pull it with the page load so it shows instantly…
What I really wanted was to dynamically pull the file so the last file that was uploaded showed on the homepage…

This is the site I am working on

dahlkecomputers.com

1 - never do this - <meta http-equiv=“refresh” content=“0; http://www.dahlkecomputers.com/home.php”, its an incomplete tag and doesn’t do anything useful.

2 - you can pull the last image uploaded by saving the moved file name to a seperate variable and then using that variable in the img tag source.

You have several options…

You can scan the directory and;

  1. check each modification date for the newest,

  2. incorporate an index number into the file name and test for the larger number.

or 3) do none of those and save a second version of the image in your images/ directory and call it lastupload.jpg. Then change your HTML page to just load lastupload.jpg. Each new upload will overwrite the last and no scanning a directory each time the page loads. This method is much kinder on the server.

Not if he wants to save those images. Its alot easier to just create a variable and set it when the file is moved after uploading.

no, you save each image as always. Just create a second image to be used in the html page. That image is replaced by the new one on each upload. This method only requires a couple of extra lines in the upload and save code. Because the image file name is hard coded in the html page, it needs no information to load the proper image, no database overhead and light and easy for the server.

Now this all changes if the user needs the file name along with the image, then your method is the proper way.

Sponsor our Newsletter | Privacy Policy | Terms of Service