PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

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

 March 15, 2022     directory, glob, lamp, php     No comments   

Issue

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?


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']))."' ..."


Answered By - Niet the Dark Absol
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing