-1

I would like to know how to load an image in php using the variable below:

<?php 

foreach (glob('cam/*.jpg') as $f) {
    # store the image name
    $list[] = $f;
}

sort($list);             # sort is oldest to newest,

echo array_pop($list);   # Newest
?>

How do I load the image file from array_pop($list)?

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
  • Load how? What does that mean? – PeeHaa Nov 04 '16 at 11:34
  • 1
  • you need to set the correct headers if you want to output an image to the browser. http://stackoverflow.com/questions/2633908/php-display-image-with-header – Cagy79 Nov 04 '16 at 11:36
  • This is HTML 101 stuff. – Funk Forty Niner Nov 04 '16 at 11:42
  • This code checks that the last image jpg added to a folder. The file name appears on the site such as cam / CAM2016110312122101.jpg Whenever a camera plays a new image in the folder, the name of the jpg file is shown on the site. I wanted to get the name of this file and open the image on the site. Html use:
    div> As you would in PHP using the variable array_pop ($ list) and taking the first element of this array (latest image).
    – Edgard de Oliveira Nov 04 '16 at 11:48
  • You can keep editing your question until it makes sense. – Cagy79 Nov 04 '16 at 11:56
  • Sorry for the english .... I have a camera that sends images every 60 seconds to a folder on a website. I used the code below to see which is the last photo sent by the camera. This code lists the name of the latest jpg file sent by the camera. It is an array. I would like to show on the site the latest picture sent by the camera. The question is, how do I get the latest image file name Mother 'array_pop (list $)' and use the 'img src' to display this image on the site? – Edgard de Oliveira Nov 04 '16 at 12:07

1 Answers1

0
<?php 

foreach (glob('cam/*.jpg') as $f) {
    # store the image name
    $list[] = $f;
}

sort($list);                    # sort is oldest to newest,

echo '<img src="'.array_pop($list).'">';

?>

  • Welcome to Stack Overflow! While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) – Scott Weldon Nov 04 '16 at 18:12