PHP 开始下载带路径的文件

问题描述

我希望 CSV 文件在我的 PHP 站点生成自动开始下载,但是,它不是下载文件,而是将文件的文本发送到站点内部的浏览器。我正在使用 PHP 手册中的代码,并尝试了无数其他 Stackoverflow 解决方案,但它总是只是将 CSV 文件的文本吐到浏览器而不是下载它。

这是代码片段:

if (isset($_POST['export'])){
   //the file is generated (excluded),there are no problems with the file
   $file = '/path/to/file/data.csv';
   if (file_exists($file)) {
     header('Content-Description: File Transfer');
     header('Content-Type: application/octet-stream');
     header('Content-disposition: attachment; filename="'.basename($file).'"');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Pragma: public');
     header('Content-Length: ' . filesize($file));
     readfile($file);
     exit;
   }
}

任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)