0

Hi All,

I was wondering if anyone could have a look at this code and explain why the page doesn't display an image? Basically I have a camer taking pictures very few minutes and I want to display the latest one.

<!DOCTYPE html>
<html>
<head>
  <title>Picture</title>
</head>

<body>
<a href="index.html">Return home</a>

<h2>Latest Image from the camera</h2>

<img src="<?php echo $path . $img ?>" alt="Latest picture" style="width:304px;height:228px" />

<?php

$root = '';
$path = 'images/';

$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);

function getImagesFromDir($path) {
    $images = array();
    if ( $img_dir = @opendir($path) ) {
        while ( false !== ($img_file = readdir($img_dir)) ) {
            // checks for gif, jpg, png
            if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
                $images[] = $img_file;
            }
        }
        closedir($img_dir);
    }
    return $images;
}

function getlastFromArray($ar) {
    $num = end($ar);
    return $ar[$num];
}

?>
</body>

</html>

I'd appreciate any pointers, thank you.

Ben

3
Contributors
3
Replies
15
Views
2 Years
Discussion Span
Last Post by Ben_8
0

You're trying to print the $img before you've created it. Also can't see getRandomFromArray() anywhere. An SQL iterator or glob() may be better than a search with preg. Preg functions are notoriously slow.

0

At line 12 $path and $img are not known yet, you declare them later.
So move the php part (line 14 -41) to the top of your script

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.