3

I'm trying to display images on the Apache server created on Pi.

PHP Code:

$files = glob("/var/www/html/Images");
for ($i=1; $i<count($files); $i++)
{
    $num = $files[$i];
    echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;";
} 

Instead of the image I get this:

I have provided all the permissions, but it doesn't work. Any help would be appreciated.

  • 1
    and what do the apache log say? – MatsK Sep 9 '17 at 17:44
  • Have you tried viewing the source? Could you edit in the source code displayed in the browser? You can view the source by right clicking and selecting 'View Page Source' or equivalent in most browsers. – Aurora0001 Sep 9 '17 at 17:44
1

There are a few errors here.

$files = glob("/var/www/html/Images");

This line doesn't make a lot of sense as written. glob returns an array matching the glob pattern (the sort of pattern you use in your terminal, e.g. /var/www/html/Images/*.jpg). Your code doesn't get an array of images, as you're expecting here.

$num = $files[$i];
echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;";

You're using the full local file path, so even with the glob being correct, you'd be getting output like so:

<img src="/var/www/html/Images/foo.jpg" alt="random image">

Clearly, you don't want the /var/www/html bit, because that path isn't accessible to browsers. You probably want to translate that to the URL path, as hosted by Apache. There are some suggestions on Stack Overflow how to handle that, which you might like to review. One of the suggestions is:

$num = str_replace($_SERVER['DOCUMENT_ROOT'], '', $num)

This should technically resolve the issue, as long as you sort out the glob. Bear in mind this isn't terribly secure or good practice, though, but it does address your immediate problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.