php 链接到默认 Web 目录之外的图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/258365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php link to image file outside default web directory
提问by user18383
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
/domain.com/public_html/www 文件夹有一个文件 index.php 索引文件中的默认 Web 目录是 /user/public_html/www 是一个包含包含函数的包含“../functions/function.inc”当我想链接到图像文件夹中的图片时,这没有问题,例如我没有得到任何结果
<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"/>
我试过 <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.JPGthe path should be http://domain.com/public_html/image/pc/tattoo_small/small_2008_10_22_001.JPGwhen I try to browse directly to this url http://domain.com/public_html/image/pc/tattoo_small/small_2008_10_22_001.JPGI 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.JPGto get to the image folder but that does not help neither.
但是当我围绕图像构建链接以获取属性时,这给了我相同的结果,我将其作为路径 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当我尝试直接浏览到这个 url 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来访问图像文件夹,但这也无济于事。
Anybody any ideas or is it impossible to html link to graphical files outside the default web directory ?
任何人有任何想法,或者不可能将 html 链接到默认 Web 目录之外的图形文件?
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.
感谢您到目前为止的答案。我将尝试使用推荐的解决方案之一解决我的问题,并在此处报告我的工作解决方案。我希望图像文件夹与 www 和 mobile 文件夹处于同一级别,因为用于 pc (www) 版本和移动版本的一些图像是相同的。当然,在 www 和 mobile 文件夹中获取图像文件夹更容易,我认为这就是我要做的。
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.
谢谢大家的建议。我不打算使用脚本的主要原因是脚本将是一个简单问题的困难解决方案,还因为我真的不知道如何将图像包装在 css 类中以及如何提供 alt图像的文本。
回答by Aron Rotteveel
It is not possible to directly access files outside of the webroot; this is a builtin security restriction that is there for good reason.
无法直接访问 webroot 之外的文件;这是一个有充分理由的内置安全限制。
It is however possible to use a PHP-script to serve these images for you. This way you can call an image like:
但是,可以使用 PHP 脚本为您提供这些图像。这样你就可以调用像这样的图像:
/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:
并使用file_get_contents()获取文件内容并将其打印到浏览器。您还应该将标头发送到客户端,在这种情况下使用 PHP 的header()函数。一个简短的例子:
<?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
- 例如,您可以跟踪下载并实施计数器
- 您可以将文件限制为经过身份验证的用户
- ... 等等
回答by ZombieSheep
If you are using Apache as the server, you can set it to alias a directory in httpd.conf...
如果您使用 Apache 作为服务器,您可以将其设置为 httpd.conf 中的目录的别名...
<IfModule mod_alias.c>
Alias /images/ "/User/Public_html/Image/"
<Directory "/User/Public_html/Image">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</IfModule>
IIRC, the aliased folder does not need to be within the webroot.
IIRC,别名文件夹不需要在 webroot 中。
回答by JamShady
You can't serve a page that's outside of the web directory because the path doesn't work, i.e. http://mydomain.com/../page.htmlsimply refers to an inaccessible location.
您无法提供位于 Web 目录之外的页面,因为该路径不起作用,即http://mydomain.com/../page.html仅指无法访问的位置。
If you really want to serve (static) files that are outside the webroot, you could write a small PHP script to read them and output them. Thus you would redirect requests to the PHP script, and the PHP would read the appropriate file from disk and return it back.
如果您真的想为 webroot 之外的(静态)文件提供服务,您可以编写一个小的 PHP 脚本来读取它们并输出它们。因此,您将请求重定向到 PHP 脚本,PHP 将从磁盘读取适当的文件并将其返回。
回答by Kornel
Create symlink inside web root that points to directory you want.
在 Web 根目录中创建指向所需目录的符号链接。
cd public_html
ln -s ../images
Apache needs Options +FollowSymlinksconfiguration directive for this to work (you may place it in .htaccessin your web root).
Apache 需要Options +FollowSymlinks配置指令才能工作(您可以将它.htaccess放在您的 web 根目录中)。
Writing PHP script that serves files from outside web root defeats the purpose of web root. You'd have to verify paths very carefully to avoid exposing entire disk to the web.
编写从外部 web 根目录提供文件的 PHP 脚本违背了 web 根目录的目的。您必须非常仔细地验证路径以避免将整个磁盘暴露给网络。
回答by Gareth
It's impossible to link directly to a file outside of the web-accessible area of the web server.
无法直接链接到 Web 服务器的 Web 可访问区域之外的文件。
However, you can write a PHP script that will proxy images for you
但是,您可以编写一个 PHP 脚本来为您代理图像
<img src="my_php_proxy.php">
because PHP can send image data as well as HTML. This PHP "image" isn't restricted to the same folder as the web accessible stuff, it can access any readable file on the server. See http://www.electrictoolbox.com/image-headers-php/for more info
因为 PHP 可以发送图像数据以及 HTML。这个PHP“图像”不限于与网络可访问内容相同的文件夹,它可以访问服务器上的任何可读文件。有关详细信息,请参阅http://www.electrictoolbox.com/image-headers-php/
回答by pilsetnieks
You can either make a link to the image directory inside the public___html, move the image directory to public_html or, if you have a particular liking towards convoluted solutions, you can write a script that reads the image file and outputs it to the user (Of course, unless you make a whitelist of all the images, you might have a potential security problem in your hands).
您可以链接到 public___html 中的图像目录,将图像目录移动到 public_html 或者,如果您特别喜欢复杂的解决方案,您可以编写一个脚本来读取图像文件并将其输出给用户(Of当然,除非您将所有图像列入白名单,否则您可能会面临潜在的安全问题)。

