Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

html - php link to image file outside default web directory

Here is the directory structure

/domain.com
 /public_html
  /functions
  /image
  /mobile
  /www

the /domain.com/public_html/www folder has a file index.php the default web directory is /user/public_html/www in the index file is an include that includes the functions with include"../functions/function.inc" this works without problem when I want to link to a picture in the image folder I don't get any results for example

<img src="../image/graphic/logo.gif" alt="alt text"/>

Does anybody has any idea why the link to the image does not work and how to link correctly to the image file ?

I tried <img src="<?php echo $_SERVER['PHP_SELF']; ?>../image/graphic/logo.gif" alt="alt text"/>

but that gives me the same result when I build a link around the image to get to the properties I get this as path http://domain.com/image/pc/tattoo_small/small_2008_10_22_001.JPG the path should be http://domain.com/public_html/image/pc/tattoo_small/small_2008_10_22_001.JPG when I try to browse directly to this url http://domain.com/public_html/image/pc/tattoo_small/small_2008_10_22_001.JPG I get an 404 file not found error because the default web directory is /domain.com/public_html/www I tried http://domain.com/../image/pc/tattoo_small/small_2008_10_22_001.JPG to get to the image folder but that does not help neither.

Anybody any ideas or is it impossible to html link to graphical files outside the default web directory ?

thanks for reading this far

Thanks for the answers so far. I will try to solve my problem with one of the recommended solutions and report my working solution back here. I wanted to have the image folder at the same level as the www and mobile folder because some of the images used for the pc (www) version and the mobile version are the same. Of course it is easier to just get an image folder in the www and in the mobile folder and I think that is what I am going to do.

thank you everybody for the advice. The main reason why I am not going to work with a script is that a script will be a difficult solution to an easy problem and also because I don't really see how you can wrap your image in a css class and how to provide alt text for an image.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not possible to directly access files outside of the webroot; this is a builtin security restriction that is there for good reason.

It is however possible to use a PHP-script to serve these images for you. This way you can call an image like:

/image.php?file=myfile.jpg

and use file_get_contents() to get the file contents and print them to your browser. You should also send the headers to the client, in this case using PHP's header() function. A short example:

<?php

    $file = basename(urldecode($_GET['file']));
    $fileDir = '/path/to/files/';

    if (file_exists($fileDir . $file))
    {
        // Note: You should probably do some more checks 
        // on the filetype, size, etc.
        $contents = file_get_contents($fileDir . $file);

        // Note: You should probably implement some kind 
        // of check on filetype
        header('Content-type: image/jpeg');

        echo $contents;
    }

?>

Using a script to this has some more advantages:

  • You can track your downloads and implement a counter, for example
  • You can restrict files to authenticated users
  • ... etc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...