Access a file which is located before / outside the server root directory?

Solution 1:

You cannot directly access any file outside your web directory. As your question includes the tag PHP as well, I assume you may want to use it.

What you can do is the following:

Inside your www directory, create a "image.php" file, with a similar content to:

<?php
  header('Content-Type: image/png');
  readfile("../img/" . $_GET['img']);
?>

And call your images with

<img src="image.php?img=myimage.png" />

Please be aware that your PHP file shouldn't be that simple :) As you may want to address multiple image formats (and providing the correct header for them), checking for malicious file path/inclusions (you don't want to use $_GET without validating/sanitizing the input), extra caching etc. etc. etc.

But this should give you an idea on how you can target your issue.

Solution 2:

It depends on what you are trying to accomplish and how. With "simple" html commands it is as you found out. You can't go to a directory outside of the www root. (for xampp applications on C for exmple it is most of the time c:\xampp\htdocs).

If you are ok with using serverside commands to accomplish what you want to achieve then you could use php to do a workaround, by reading the appropriate files in via PHP. For example if your file is named "myimg.gif" and lies in "c:\pics"

<img SRC="data:image/gif;base64,<?php echo base64_encode(file_get_contents("c:\pics\myimg.gif"));?>">

With that you are reading the contents of the file and writing it directly into the img tag. Be aware that you need to change image/gif to what you really need there.