【问题标题】:Downloading A File Located Above public_html下载位于 public_html 之上的文件
【发布时间】:2013-12-11 20:22:17
【问题描述】:

出于安全原因,我在 public_html 上方的文件夹中托管了一个文档 - 不能将文件移动到 Web 根目录。

我正在尝试通过 readfile() 访问它们,但无论我下载什么,最终都是 0 字节和一个空文件。我认为问题在于“Content-Length”标头没有得到正确的值,因为 filesize($target) 没有给出输出。

有什么想法吗?包含的代码:

header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file_url);

【问题讨论】:

  • 如果您没有从filesize() 获得输出,那么您的路径可能是错误的。
  • 刚刚测试以确保路径正确 - 确实如此。还有其他想法吗?
  • 那么,你有权限问题。
  • filesize() 不能访问上面目录中的文档吗?
  • filesize() 和所有 PHP 函数都可以访问您授予用户 PHP 正在运行的访问权限。

标签: php file header download


【解决方案1】:

我没有在您的代码中看到 $file_url 定义。也许应该是$file

header('Content-Description: File Transfer');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
$file="../docs/$c/$path";
$size=filesize($file); //i think this line is the problem
header ("Content-Length: $size");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($file);

我也没有看到 $filename 定义。

【讨论】:

  • 文件名在代码的较高位置定义 - 该变量正常工作。 file_url 应该是文件 - 这也是正确的。
  • 如果没有标头,您可以readfile($file) 并仅输出原始代码吗?如果您不能,这可能是@Brad 指出的权限问题。您可能想尝试将文件复制到 Web 文件夹,然后将其删除...但这效率低下...
猜你喜欢
  • 2012-07-25
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多