3

In this project, I am uploading admin submitted files to a folder which is outside of the public folder.

/web/ (the public folder)
/upload/ (uploading image to this which is at the same level as the public folder)

Now allthough I am able to uplaod using the absolute url (/www/userid/upload/), I am unable to echo the same image using either relative or absolute url.

I have tried <img src="/www/userid/upload/photo.jpg" /> as well as <img src="../upload/photo.jpg" /> but none is working. Any hints ?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Jeremy
  • 975
  • 4
  • 15
  • 26
  • 1
    possible duplicate of [php link to image file outside default web directory](http://stackoverflow.com/questions/258365/php-link-to-image-file-outside-default-web-directory) **or** http://stackoverflow.com/questions/19979537/how-can-i-show-the-images-outside-the-web-root-directory-in-my-php-application **or** http://stackoverflow.com/questions/11270931/displaying-images-from-outside-the-site-root **or** http://stackoverflow.com/questions/17750639/how-to-view-an-image-outside-of-web-root – j08691 Dec 10 '14 at 13:48
  • You cannot have a web reference outside your public folder, you have to include "uploads" as a virtual directory in your web server. – cardeol Dec 10 '14 at 13:48

3 Answers3

7

direct path won't work because you can't get content from outside of root folder.. you can do it another way, Read image file in php script from server dir path and serve file using header

script file.php

<?php
  $mime_type = mime_content_type("/image_path/{$_GET['file']}");
  header('Content-Type: '.$mime_type);

  readfile("/image_path/{$_GET['file']}");
?>


<img src="file.php?file=photo.jpg" />
Girish
  • 11,907
  • 3
  • 34
  • 51
  • 1
    Thanks @Girish. How you will do if you want to have multiple image types, like jpg, jpeg, png, x-png, gif, etc ? – Jeremy Dec 10 '14 at 13:59
2

You normally do not access directories outside web root.

A quick solution is to add a alias in apache config like

alias /upload/ /www/userid/upload/

inside the virtualhosts

and use

/upload/photo.jpg
rjv
  • 6,058
  • 5
  • 27
  • 49
0

I would skip the php and folder options and use symbolic links directly in the server. I assume that you are using some form of linux as your server. So, if it is not a security issue, simply make the upload folder a copy of the public folder that you have the images in. This, will, however, make everything in that folder appear in the upload folder, but this should not be much of an issue, as the alternative would be to alter permissions on the upload folder and script the whole thing out that way.

morantis
  • 106
  • 8