user238021
user238021

Reputation: 1221

Displaying html page with image with different image names

I have a script that generates different jpg file name every day. For example, today it will generate "total_1.jpg" and tomorrow it could generate "total_2.jpg" but the format would be "total_".jpg. I need to display the latest image on a daily basis in a website. I can't use the usual "img src" html code as i dont know what would be the filename for that day. So I am experimenting with php. Before my script runs, I remove the jpg file from previous day so at any point, there should be only 1 jpg file (latest). I wrote the following test code

    <html>
    <head>

    </head>
    <body>

    <?php
$files = array();
$id = "total_";
$files = glob("id*.jpg");

echo "<img src='$files[0]' />";
    ?>

    </body>
    </html>

When I open the browser and go to http:///test.php, all I get is "; ?>

What am I doing wrong ? This is on a apache server and i dont see any error under /var/log/apache2/error.log.

Upvotes: 1

Views: 109

Answers (1)

BigScar
BigScar

Reputation: 343

This code works:

<html>
<head>
</head>
<body>

<?php
$files = array();
$id = "total_*.jpg";
$files = glob($id);

echo "<img src='$files[0]' />";
?>
</body>
 </html>

Your main problem was shifting from variable $id to glob("id*.jpg) on the next line, leaving off the "$". All the rest of your syntax and logic was correct. You might think about cases where your files are not named as neatly as you like and/or you have multiple files in your directory and don't always want the first one.

Upvotes: 1

Related Questions