php链接到默认web目录之外的图像文件

use*_*383 15 html php image

这是目录结构

/domain.com
 /public_html
  /functions
  /image
  /mobile
  /www
Run Code Online (Sandbox Code Playgroud)

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

<img src="../image/graphic/logo.gif" alt="alt text"/>
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么图像链接不起作用以及如何正确链接到图像文件?

我试过了 <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文件错误,因为默认的网络目录是/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图像的文字.

Aro*_*eel 32

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

但是,可以使用PHP脚本为您提供这些图像.这样你可以调用如下图像:

/image.php?file=myfile.jpg
Run Code Online (Sandbox Code Playgroud)

并使用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;
    }

?>
Run Code Online (Sandbox Code Playgroud)

使用脚本可以获得更多优势:

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

  • 你的脚本有一个主要的安全漏洞:它应该使用类似$ file = basename(urldecode($ _ GET ['file']))的东西.否则,这可能会起作用:/image.php?file =../../../../../../../../etc/passwd (4认同)

Zom*_*eep 16

如果您使用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>
Run Code Online (Sandbox Code Playgroud)

IIRC,别名文件夹不需要在webroot中.


Jam*_*ady 9

您无法提供位于Web目录之外的页面,因为路径不起作用,即http://mydomain.com/../page.html只是指无法访问的位置.

如果你真的想要提供webroot之外的(静态)文件,你可以编写一个小的PHP脚本来读取它们并输出它们.因此,您将请求重定向到PHP脚本,PHP将从磁盘读取相应的文件并将其返回.

  • 并且你想要真正做到这一点,或者你将引入一个巨大的安全漏洞:) (3认同)

Kor*_*nel 5

在Web根目录中创建指向所需目录的符号链接.

cd public_html
ln -s ../images
Run Code Online (Sandbox Code Playgroud)

Apache需要Options +FollowSymlinks配置指令才能工作(您可以将其放在.htaccessWeb根目录中).

编写从外部Web根目录提供文件的PHP脚本会破坏Web根目录.您必须非常仔细地验证路径,以避免将整个磁盘暴露给Web.