2

更新

刚刚从我的错误日志中发现有readfile() has been disabled for security reasons任何替代方法readfile()吗?将fopenfread使用 zip 文件?

==================================================== =================================

我的脚本:

<?php

$str = "some blah blah blah blah";
file_put_contents('abc.txt', $str); // file is being created
create_zip(array('abc.txt'), 'abc.zip'); // zip file is also being created

// now creating headers for downloading that zip

header("Content-Disposition: attachment; filename=abc.zip");
header("Content-type: application/octet-stream; charset=UTF-8");    
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header('Content-Transfer-Encoding: binary'); // added this line as per suggestion
header('Content-Length: ' . filesize("abc.zip")); // added this line as per suggestion
readfile("abc.zip");
//echo 'do something'; // just for testing purpose to see if code is running till the end
exit;

当我运行上面的脚本时,我得到一个空白页(没有下载提示)。当我取消注释“做某事”行时,我会在屏幕上看到它。所以脚本一直运行到最后一行。

我也把它放在error_reporting(E_ALL)页面的顶部,但什么都没有出现。

我在这里想念什么?

4

2 回答 2

1

尝试添加Content-Length标题。有关完整示例,请参阅PHP readfile()文档。

于 2013-09-13T08:42:10.210 回答
0

一种替代方法readfile()是指向ZIPecho文件本身的链接,人们可以简单地单击它,然后系统会提示您保存文件。

使用:echo "<a href='$filename'>File download</a>";

PHP

<?php
$str = 'some blah blah blah blah';
$zip = new ZipArchive();
$filename = "abc.zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE)==TRUE) {
$zip->addFromString("abc.txt", $str);
$zip->close();
}

echo "<a href='$filename'>File download</a>";
exit();
?>

这通常是压缩文件然后打开提示的方式save file as...

<?php
ob_start();
$str = 'some blah blah blah blah';

$zip = new ZipArchive();
$filename = "abc.zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
   exit("cannot open <$filename>\n");
}

$zip->addFromString("abc.txt", $str);
$zip->close();

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");

clearstatcache();
header("Content-Length: ".filesize('abc.zip'));

ob_flush();
readfile('abc.zip');
?>
于 2013-09-13T16:48:37.793 回答