可能重复:
php, file download
我有一些不在Web根目录中的文件,我需要提供下载。所以我有一个脚本,使用下面的代码来下载所请求的文件。问题是,我下载的每个文件都已损坏?文件没问题,因为如果我使用FTP下载,它们会打开。以下是传递的标题:
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for i.e.
header("Content-Type: " . $download[0]['mime']);
header("Content-Disposition: attachment; filename=" .$download_file);
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
readfile($attachment_location);
答案 0 :(得分:8)
以下是用于此的标题示例:
http://php.net/manual/en/function.readfile.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));