0

我需要在 .zip 中制作一个 zip 文件index.php。我的代码是:

$zip = new ZipArchive();

$DelFilePath="first.zip";

if(file_exists($_SERVER['DOCUMENT_ROOT']."/admin/Allorders/".$DelFilePath)) {

        unlink ($_SERVER['DOCUMENT_ROOT']."/admin/Allorders/".$DelFilePath); 

}
if ($zip->open($_SERVER['DOCUMENT_ROOT']."/admin/Allorders/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
        die ("Could not open archive");
}
    $zip->addFile($_SERVER['DOCUMENT_ROOT']."/admin/index.php","file.php");

// close and save archive


$file=$_SERVER['DOCUMENT_ROOT']."/admin/Allorders/".$DelFilePath;

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($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: ' . filesize($file));

我得到一个 0 字节的 zip 文件。我的代码有什么问题?

4

1 回答 1

2

声明所有标题后,您需要回显实际数据。所以:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($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: ' . filesize($file));
echo $data;

其中 $data 是您的 zip 存档的内容。

还可以在 zip 上尝试一些 var_dump() 以查看它在内存中是否正常。

于 2013-11-14T14:41:22.177 回答