glob() is successfully identifying files, but I get a "404" when trying to access them?

StackOverflow https://stackoverflow.com/questions/23595803

  •  20-07-2023
  •  | 
  •  

Question

My local LAMP environment is using AMPPS for apache. My project directory is structured like so:

public/
    index.php
    css/
    js/
    img/
        layout/
            banners/
                banner1.jpg
                banner2.jpg
                banner3.jpg
src/
    config.php
    library/
    templates/
        header.php
        footer.php
        nav.php

inside header.php I have the following code:

  <ul class="slideshow">
    <?php
        $images = glob( $GLOBALS["config"]["paths"]["images"]["layout"] . "/banners/*.jpg" );
        foreach($images as $image){
          $out = "<li><img alt='banner' width=1280 height=320 src='".$image."'/>ASC Online</li>";
          ChromePHP::log($out);
          echo $out;
        }
    ?>
  </ul>

( the global $config is set in the src/config.php file. )

When the code is echoed out, it appears to be correct:

<li><img alt='banner' width=1280 height=320 src='/Users/Tom/Web/dev/asconline.local/public/img/layout/banners/banner1.jpg'/>ASC Online</li> 
<li><img alt='banner' width=1280 height=320 src='/Users/Tom/Web/dev/asconline.local/public/img/layout/banners/banner2.jpg'/>ASC Online</li> 
<li><img alt='banner' width=1280 height=320 src='/Users/Tom/Web/dev/asconline.local/public/img/layout/banners/banner3.jpg'/>ASC Online</li>

If I go to the URL indicated in the output above, the image files are there... The directory is correct.

However, in-browser, I am getting a 404: File Not Found when the browser tries to retrieve the files.

It is not permissions because I am able to get other files/images in the project dir.

Any ideas?

Était-ce utile?

La solution

Your glob is returning paths relative to the drive root. When accessing through the browser, it is done relative to the document root, which is most likely the public_html folder.

You will need to do something like:

"... src='".substr($image,strlen($_SERVER['DOCUMENT_ROOT']))."' ..."
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top