我的问题是我的浏览器下载脚本将文件内容写入浏览器,不要将其作为文件从浏览器下载。
我的提供者是Strato。 (www.strato.de)
我使用这个php脚本下载文件:
if(file_exists($backup_file_path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($backup_file_path).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($backup_file_path));
ob_clean();
flush();
readfile($backup_file_path);
exit;
echo "<script>console.log('Download success');</script>";
}
在带有xampp的localhost上它正在运行,但在Strato我遇到了我所描述的问题。
如果可以,请帮助我。 谢谢!
PS: PHP版本:5.5
答案 0 :(得分:0)
我看到了......解决这个问题,但你的问题是另一个......
改变这个:
exit;
echo "<script>console.log('Download success');</script>";
用这个:
echo "<script>console.log('Download success');</script>";
exit;
我建议禁止此行echo "<script>console.log('Download success');</script>";
,可能会干扰下载文件...
要解决您的问题,您可能需要试试这个......
$file = "test.zip";
$downloads_folder = "files/";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . sprintf("%u", filesize($downloads_folder . $file)));
$fh = fopen($downloads_folder . $file, "rb");
while (!feof($fh)) {
echo fgets($fh);
flush();
}
fclose($fh);
exit;
让我知道,如果适合您,经过测试,适用于我在Linux和Windows上运行php 5.4和5.6