../图像路径

0 html

我试图访问位于我的网站外的文件夹中的图像.我试图通过放置../来超出我的文件夹,但这样做有误.我也尝试给出一个绝对路径,但是那里有空格(比如"文档和设置"),这也行不通

有人有想法吗?

Pau*_*xon 6

您无法从客户端HTML访问文档根目录之外的文件.如果无法在文档根目录下移动文件,则可以将服务器配置为允许访问该目录,或者编写服务器端脚本以中继文件.

重新配置服务器...

如果您正在使用Apache,则可以使用Alias将文件系统上的目录映射到文档根目录:

Alias /foo "/path/to/my/foo"
Run Code Online (Sandbox Code Playgroud)

...或者写一个中继脚本

B计划是编写一个脚本来获取文件.例如,而不是尝试类似的东西

 <img src="/../foo/bar.gif">
Run Code Online (Sandbox Code Playgroud)

你可以做到这一点

 <img src="/getimage.php?name=bar.gif">
Run Code Online (Sandbox Code Playgroud)

getimage.php的简单实现可能是这样的

//get the passed filename
$file=$_GET['name'];

//basic security check - ensure the filename is something likely
if (preg_match('/^[a-z]+\.gif+$/', $file))
{
    //build path and check it exists
    $path=$_SERVER['DOCUMENT_ROOT'].'/../foo/'.$file;
    if (file_exists($path))
    {
         //return file here. A production quality implementation would
         //return suitable headers to enable caching and handle
         //If-Modified-Since and etags...

         header("Content-Type: image/gif");
         header("Content-Length:".filesize($path));
         readfile($path);
    }
    else
    {
          header("HTTP/1.0 404 Not found");
          echo "Image not found";
    }

}
else
{
    header("HTTP/1.0 400 Bad Request");
    echo "Naughty naughty";
}
Run Code Online (Sandbox Code Playgroud)