您无法从客户端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)