Jump to content


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


Image Display Script Help

Recommended Posts

Greetings everyone,
What I have is a server with some web cam software installed. The software saves images whenever motion is detected. I have an apache/php server going to serve a php page that I want to display the images. Its just a side DIY project at my home. Here is what I have so far.

$dir = ".";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (preg_match("/.jpg/", $file)) {
                $exif = exif_read_data($file, 0, true);
                echo "<tr>";
                echo "<td valign=top><a href=$dir/$file><img src=$dir/$file width=250 height=200></a><td>";
                echo "<td valign=top><font size=-1>";
                echo "File: <b>" . $exif['FILE']['FileName'] . "</b><br/>";
echo "Size" . ': ' . filesize($file) . ' bytes' . "<br/>";
echo date ("F d Y H:i:s.", filemtime($file));
                echo "</font></td>";
                echo "</tr>";

I am trying to do 2 things that I haven't been able to figure out. 1) I don't want to display any images will a filesize less than 100000 bytes. 2) I want to order the images by their creation date with the most recent at the top. I just have all the images and the script in the root directory of my web sever. Any help with figuring this out would be great.

Thank you in advance,

Share this post

Link to post
Share on other sites
You already know about filesize(), so why aren't you using it to filter the results?

if (preg_match('/\.jpg$/',$file) && filesize($file) > 100000) {
    // ....

If you need to sort data, you can't do it on the fly.  You can save it in an array and use the array sorting functions to rearrange the data in the order you want it.  Then it can be output in the desired order.

For example:


function _filesorter($a,$b) {
    if ($a['date'] == $b['date']) return 0;
    return ($a < $b) ? -1 : 1;

$imageFiles = array();
while (($file = readdir($dh)) !== false) {
    if (preg_match('/\.jpg$/',$file)) {
        $exif = exif_read_data($file,0,true);
        $imageFiles[$file]['date'] = filemtime($file);
        $imageFiles[$file]['size'] = filesize($file);
        $imageFiles[$file]['name'] = $exif['FILE']['FileName'];
        $imageFiles[$file]['path'] = "$dir/$file";

// ...


// ... then print the output ...


I haven't tested that, but you get the idea.  You need the extra callback function to sort the multi-dimensional array by one of its values.

(Notice the updated regex pattern.  You need to escape the period (\.) to actually match a period, and the pattern should also be anchored at the end ($).  Your pattern will match *.jpg files, but it will also match "about_jpgs.html," etc.)

Share this post

Link to post
Share on other sites


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.