且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP链接到默认Web目录之外的图像文件

更新时间:2023-02-19 21:53:20

无法直接访问webroot之外的文件;这是一个内置的安全限制,有很好的理由。



然而,可以使用PHP脚本为您提供这些图像。通过这种方式,您可以调用像:

  /image.php?file=myfile.jpg 

并使用 file_get_contents()获取文件内容并将其打印到浏览器中。您还应该将标题发送到客户端,在这种情况下使用PHP的标题()功能。一个简短的例子:

 <?php 

$ file = basename(urldecode($ _ GET [ '文件']));
$ fileDir ='/ path / to / files /';

if(file_exists($ fileDir。$ file))
{
//注意:您应该对文件类型,大小做更多的检查
// //等等
$ contents = file_get_contents($ fileDir。$ file);

//注意:你可能应该对filetype
header('Content-type:image / jpeg')执行一些
检查。

echo $ contents;
}

?>

使用脚本来获得更多优势:


  • 您可以跟踪您的下载并实施计数器,例如 li> ...等


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.

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