2

我无法从“public_html”外部向用户上传文件,这是每个人都可以通过 http 访问的文件夹。前任。/ 之后的 www.website.com/everyhting 是 public_html。相信大家都知道我指的是什么。

所以我有这个脚本应该从(服务器视角)/images/protected/读取文件(sample.pdf),但PHP找不到我的文件。我做错了吗?

<?php
$file = '/images/protected/sample.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
} else {
    echo "404 - FILE NOT FOUND!<br />\n";
    echo "File: $file";
}
?>

每次执行此脚本时,我都会404 - FILE NOT FOUND!<br />\n下载文件而不是文件。

4

2 回答 2

4

您必须提供绝对系统路径。目前,您在 请求文件/images/...,而不是/var/www/hosts/whateverdomain/images/....

$file = $_SERVER['DOCUMENT_ROOT'].'/images/protected/sample.pdf';

如果您的文件确实位于/images/protected/,请确保您有足够的权限来读取该文件。

于 2011-10-24T20:31:56.377 回答
0

原来我在共享主机上,所以我网站的正确“根”路径是 /home/me/

于 2011-10-28T15:11:27.240 回答