微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP 开始下载带路径的文件

如何解决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 举报,一经查实,本站将立刻删除。