3

I am trying to load in an image in a <img> tag within HTML. My images folder is in the same folder as the root folder, meaning I need to go up one folder to access the images folder.

I looked around the internet and found the method using GET requests within the src attribute from the image, unfortunately without success.

Folder Structure:

img >
    somefile.png
    foo.png
    bar.png
html >
    index.php
    imageRequest.php
    //other root files

I cannot use the following: <img src="../img/somefile.png"> because it points towards to something non existant (duh!). So I attempted to use the GET request.

My HTML

<div>
   <img src="imageRequest.php?requested=somefile.png">
</div>

My PHP (imageRequest.php)

    $fileName = $_GET['requested'];
    fetchImage();

    function fetchImage(){
        global $fileName;
        if(!isset($fileName)) return;
        $filePath = dirname($_SERVER['DOCUMENT_ROOT'])."/img/".$fileName;
        if(file_exists($filePath)){
            readfile($filePath);
        }
    }

TL;DR: Use image from folder outside the root folder within the <img> tag.

2
  • 1
    since your folder is outside of your root directory, you can't use DOCUMENT_ROOT... if you have the rights you could use absolute paths in your function fetchImage(). i suggest also, that you pass a variable $fileName to your function and don't use global there. Commented May 9, 2016 at 8:14
  • @RaphaelMüller dirname($dir); grabs the parent folder, which is the folder I need ;). Thanks for the tip though. Commented May 9, 2016 at 8:32

1 Answer 1

4

Get that image using PHP as base64 image and output that content along with proper headers:

[imageRequest.php?requested=some_file.png]

$imagesDir = __DIR__.'/../';
$content = file_get_contents($imagesDir.$_GET['requested']);
$content = base64_encode($content);

$data = "data:image/png;base64,{$content}";

header('Content-Type: image/png');
echo base64_decode($data);`
Sign up to request clarification or add additional context in comments.

7 Comments

please be aware that you can easy inject another path. you have to check if it's really an image. otherwise it could be possible to access config files or so...
@RaphaelMüller Yes, my code does not contain any security checks. OP must check for valid image name, valid get parameter, validate if image exists and so on
it could also be nice to have a rewrite rule, so that in the html source it's easier to use. e.g. /someimagesource/some_file.png gets rewritten to imageRequest.php?requested=some_file.png
@RaphaelMüller It's not related with OP question. But as side note you can map that any file with extension png|jpg|jpeg|png would lead to this function and than url may look like example.com/some_file.png
@Justinas The part between the '[' is supposed to be in the src="" right? At the moment it doesn't seem to work though. It shows the 'no image' icon.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.