【发布时间】:2012-12-11 01:41:37
【问题描述】:
<?php
$error = "";
$file_folder = "temp/";
// folder to load files
if (extension_loaded('zip')) {
// Checking ZIP extension is available
$zip = new ZipArchive();
// Load zip library
$zip_name = "images_".date("d-m-Y") . ".zip";
// Zip name
if ($zip -> open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../uploads/"));
$files = iterator_to_array($iterator, true);
foreach ($iterator as $file) {
$zip -> addFile($file_folder . $file);
// Adding files into zip
}
print_r($zip);
//echo $zip_name;
if (file_exists($file_folder.$zip_name)) {
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $zip_name . '"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
} else {
echo "error";
}
$zip -> close();
//} else
// $error .= "* Please select file to zip ";
} else
$error .= "* You dont have ZIP extension";
?>
我尝试输出 $zip 对象并得到以下响应: ZipArchive 对象([status] => 0 [statusSys] => 0 [numFiles] => 12 [filename] => /var/www/bigb/ajax/images_24-12-2012.zip [comment] =>)
状态为零说明创建 zip 时没有错误(参考:http://www.php.net/manual/en/zip.constants.php)
我参考了以下帖子: 1)Download all images from a single directory of a website 2)How to create a zip file using php and delete it after user downloads it? 3)http://www.9lessons.info/2012/06/creating-zip-file-with-php.html
第三个对我帮助最大,另外两个我的状态是 23。
问题:我可以创建一个 zip 并且下载它没有问题,但是当我打开 zip 时,我看不到里面的任何文件,但 zip 有 mbs 中的内存。
请帮帮我...
更新:由于 print_r / echo 和/或试图覆盖同一文件而发生错误 23。
Update2:问题已解决。这是由于 RecursiveDirectoryIterator 中的路径(即 ../uploads/),一旦我将代码移动到主文件夹并将路径更改为(uploads/),一切都开始正常工作。赞!!!
【问题讨论】: