php 链接到默认 Web 目录之外的图像文件 [英] php link to image file outside default web directory

查看:25
本文介绍了php 链接到默认 Web 目录之外的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是目录结构

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

/domain.com/public_html/www 文件夹有一个文件 index.php默认的 Web 目录是/user/public_html/www索引文件中有一个包含函数包括../functions/function.inc"这没有问题当我想链接到图像文件夹中的图片时,我没有得到任何结果例如

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 ?

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

但这给了我相同的结果当我围绕图像建立链接以访问属性时,我将其作为路径http://domain.com/image/pc/tattoo_small/small_2008_10_22_001.JPG路径应该是http://domain.com/public_html/image/pc/tattoo_small/small_2008_10_22_001.JPG当我尝试直接浏览到这个网址时http://domain.com/public_html/image/pc/tattoo_small/small_2008_10_22_001.JPG我收到 404 文件未找到错误因为默认的 web 目录是/domain.com/public_html/www我试过http://domain.com/../image/pc/tattoo_small/small_2008_10_22_001.JPG进入图像文件夹,但这也无济于事.

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.

任何人有任何想法或无法将 html 链接到默认 Web 目录之外的图形文件吗?

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

感谢阅读到这里

感谢到目前为止的答案.我将尝试使用推荐的解决方案之一解决我的问题,并在此处报告我的工作解决方案.我希望图像文件夹与 www 和 mobile 文件夹处于同一级别,因为用于 pc (www) 版本和移动版本的一些图像是相同的.当然,在 www 和 mobile 文件夹中获取图像文件夹更容易,我认为这就是我要做的.

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.

谢谢大家的建议.我不打算使用脚本的主要原因是脚本将是一个简单问题的困难解决方案,还因为我真的不知道如何将图像包装在 css 类中以及如何提供 alt图片的文字.

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.

推荐答案

无法直接访问 webroot 之外的文件;这是一个有充分理由的内置安全限制.

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

然而,可以使用 PHP 脚本为您提供这些图像.这样你就可以调用像这样的图像:

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

并使用 file_get_contents() 获取文件内容并将它们打印到您的浏览器.您还应该将标头发送到客户端,在这种情况下使用 PHP 的 header() 函数.一个简短的例子:

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;
    }

?>

使用脚本有更多优势:

  • 例如,您可以跟踪下载并实施计数器
  • 您可以将文件限制为经过身份验证的用户
  • ...等

这篇关于php 链接到默认 Web 目录之外的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆