PHP链接到默认Web目录之外的图像文件 [英] php link to image file outside default web directory
问题描述
这是目录结构
/domain.com
/ public_html
/函数
/ image
/ mobile
/ www
索引文件中的默认网络目录是/ user / public_html / www
是包含
包含函数的包含.. /functions/function.inc
这个工程没有问题
当我想链接到图片文件夹中的图片时我没有得到任何结果
例如
< img src =../ image / graphic / logo.gifalt =alt text/>
有谁知道为什么图片链接不工作,以及如何正确链接到图片文件?
我试过< img src =<?php echo $ _SERVER ['PHP_SELF'];?> .. /image/graphic/logo.gifalt =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文件错误
,因为默认的网页目录是
/domain.com/public_html/www
我试过
http://domain.com/../image /pc/tattoo_small/small_2008_10_22_001.JPG
到达图片文件夹,但这两者都没有帮助。
任何人有任何想法或是否不可能将html链接到默认网页目录之外的图形文件?
感谢您阅读此内容
感谢到目前为止的答案。
我将尝试用推荐的解决方案之一解决我的问题,并将我的工作解决方案报告回来。
我想让图像文件夹与www和移动文件夹处于同一级别,因为用于pc(www)版本和移动版本的一些图像是相同的。
当然,在www和移动文件夹中获取图片文件夹比较容易,我认为这就是我要做的。
感谢大家的建议。我不打算使用脚本的主要原因是脚本将是一个简单问题的难题解决方案,也是因为我真的不知道如何将您的图像包装在css类中,以及如何提供alt文本为图片。
无法直接访问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
这篇关于PHP链接到默认Web目录之外的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!