【问题标题】:Readfile returns an empty fileReadfile 返回一个空文件
【发布时间】:2019-07-10 08:15:05
【问题描述】:

我有这个代码来强制下载,$file 是现有 .jpg、.png 或 .pdf 文件的 url(我确定它存在)

 <?php
    $file = $_REQUEST['file'];
    $file_extension = end(explode('.', $file));
    $file_name = end(explode('/', $file));
    switch ($file_extension) {
        case 'jpg':
            header('Content-Type: image/jpeg');
            header('Content-Disposition: attachment; filename='.$file_name);
            header('Pragma: no-cache');
            readfile($file);
            break;
        case 'png':
            header('Content-Type: image/png');
            header('Content-Disposition: attachment; filename='.$file_name);
            header('Pragma: no-cache');
            readfile($file);
            break;
        case 'pdf':
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename='.$file_name);
            header('Pragma: no-cache');
            readfile($file);
            break;
    }

但它正在下载一个空 (0KB) 文件(名称正确)

有没有想过会发生什么?

【问题讨论】:

  • 您确定$file 是现有的吗?如:可从您当前的位置访问? IE。如果$filedir/file.jpg,但您的脚本在/something/x/ 中运行,而目录dir/something/x/y/dir 中,则路径将无法解析。您可以通过var_dump(file_exists($file)); exit; 快速了解。
  • 是的,正如我在问题中提到的,我确保它们存在(这是一个工作图像/pdf URL)
  • 如果你尝试 var_dump(file_get_contents($file)) 你可以得到内容吗?
  • @FrancescoMineo 这很奇怪.. 它打印出bool(false)
  • 为什么不将请求重定向到 URI? readfile() 主要不是为了读取远程文件,它只是一个奖励。

标签: php file download readfile


【解决方案1】:

由于 file_get_contents() 也返回 null,因此您的问题可能是 php.ini 配置中的设置。

参数allow_url_fopen必须是On

【讨论】:

  • 对不起,website.com/download.php 是这段代码所在的文件,所以它不是下载文件的一部分
  • 如果您禁用了“allow_url_fopen”选项,请检查您的 PHP INI。您必须启用它。
  • 我想一定是这样。我会检查并告诉你
  • 就是这样,如果你想编辑答案,我可以接受它;)
【解决方案2】:

那是因为您缺少 Content-Length 标头。

试试这个:

$file = $_REQUEST['file'];
$file_extension = end(explode('.', $file));
$file_name = end(explode('/', $file));

switch ($file_extension) {
    case 'jpg':
        header('Content-Type: image/jpeg');
        break;
    case 'png':
        header('Content-Type: image/png');
        header('Content-Disposition: attachment; filename='.$file_name);
        break;
    case 'pdf':
        header('Content-Type: application/pdf');
        break;
}

header('Content-Disposition: attachment; filename='.$file_name);
header('Pragma: no-cache');
header('Content-Length: ' . filesize($file));

// You may want to add this headers too (If you don't want the download to be resumable - I think).
header('Expires: 0');
header('Cache-Control: must-revalidate');

// And you may consider flushing the system's output buffer if implicit_flush is turned on in php.ini.
flush();

// If you have the file locally.
readfile($file);

// Otherwise,
echo file_get_contents($file); // You should have allow_url_include on in php.ini

没试过,但应该可以。

【讨论】:

  • 感谢您的宝贵时间,但问题仍然存在
  • 如果它不起作用,那么您可能需要检查文件本身。您确定该文件与您的脚本存在于同一目录中吗?因为这就是您的原始代码所暗示的。
  • 我认为问题是即使它在同一个主机中,也不是路径而是URL
  • 我现在将更新我的答案以处理这种情况。
  • 它不起作用,因为file_get_contents($file) 返回 false.. 并且反对票不是我的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多