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

Recommended Answers

All 3 Replies

Member Avatar for diafol

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.

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

Thank you for the pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.