【问题标题】:readfile download a blank/empty filereadfile 下载一个空白/空文件
【发布时间】:2015-01-28 00:32:22
【问题描述】:

我正在尝试使用 readfile 从服务器下载文件。

该文件确实存在,当我直接在浏览器中使用该 URL 时,它会按预期下载正确的文件。

但是,当我使用以下代码执行此操作时,它会下载具有正确文件名的文件,但内容完全为空白,并且字节数为零。

我想知道这段代码是否真的不正确,而是创建了一个新文件,这当然是空白的。

function downloadFile($filename){
    $downloadroot = 'http://my.url.co.uk/exports/'.$filename.'.csv';
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename.'.csv');
    header('Pragma: no-cache');
    readfile("$downloadroot");
}

这是什么原因?是否有其他方法可以从 URL 下载文件而无需创建某种“目标另存为”链接?

【问题讨论】:

    标签: php download readfile


    【解决方案1】:

    您可以使用 file_get_contents() 查看http://php.net/file_get_contents 了解完整详情。

    或者,如果您尝试读取的只是 csv: http://php.net/manual/en/function.fgetcsv.php

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 2016-10-16
      • 2020-08-07
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多