[英]PHP readfile and download not working
我正在尝试编写一个PHP脚本,该脚本将在客户端下载文件。 该文件可以是Excel(xlsx)或JSON。
The URL will look like:
http://<server>/php/download.php?file=/some/path/file.xlsx&name=file.xlsx&format=xlsx
我的PHP脚本如下所示:
<?php
if($_GET['format']==='excel') {
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else if($_GET['format']==='json') {
header('Content-type: application/json');
}
header("Content-disposition: attachment; filename=".$_GET['name']);
readfile($_GET['file']);
?>
我已仔细检查了实际文件是否未损坏或任何内容。 但是,当我使用上面的URL时,将下载大小为0的文件(尽管名称正确),这显然是错误的。 我的PHP代码有什么问题?
我能够使用echo file_get_contents($filename)
而不是由服务器管理员禁用的readfile
来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.