我正在尝试下载文件夹my_folder
中的文件。在文件上制作zip文件并使用php下载它。这是我试过的。
<?php
$path = '/my_folder/path/';
$handle=opendir($path);
$files = array();
while(false!==($file = readdir($handle)))
{
$files[] = $path . $file;
}
$zipname = 'file.zip';
$zip = new ZipArchive;
if($zip->open($zipname, ZipArchive::CREATE) == TRUE){
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipname));
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipname);
unlink($zipname);
?>
看起来效果非常好。它下载了zip文件但是当我尝试解压缩时,它会给出密码对话框,并显示错误消息Archieve non readable; Would you like to try a password
任何人都可以告诉我代码中的错误或如何避免这种对话并提取文件吗?