在 php 中创建 zip 和下载在 Mozilla 浏览器中不工作
Creating zip and download in php not working in Mozilla Browser
我正在尝试创建一个 zip 文件并使用 ZipArchive 库通过 PHP 脚本下载它。
$post['files']
此变量包含 URL 托管在 Amazon S3 上的图像,因此我首先将它们下载到我的服务器,创建 zip 文件并下载它们。
我当前的代码在 chrome 上工作正常 我也在 Safari 和 Opera 上测试过,它工作正常但是当我尝试在 Mozilla Firefox 上做同样的事情时,它现在工作了。
请看看我的php脚本
<?php
if(isset($_POST['createZip'])){
$post = $_POST;
$file_folder = "ZipFIle"; // folder to load files
//downloading on my server
foreach($post['files'] as $file){
$ch = curl_init($file);
$fp = fopen($file_folder.basename($file), 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
if(extension_loaded('zip')){ // Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0){
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = "ZipFIle.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";
}
$dir = new DirectoryIterator("ZipFIle");
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
//var_dump($fileinfo->getFilename());
$imgfile = $fileinfo->getFilename();
foreach($post['files'] as $file){
if ( basename($file) == $imgfile) {
//get file and add it to zip
$zip->addFile($file_folder.$imgfile);// Adding files into zip
}
}//foreach($post['files'] as $file)
}//if
}
$zip->close();
if(file_exists($zip_name)){
//echo $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);
foreach ($dir as $fileInfo) {
if(!$fileInfo->isDot()) {
//delete all files after creating zip
unlink($fileInfo->getPathname());
}
}//foreach for delete
}else{echo "no zip";}
}else
$error .= "Please select file to zip";
}else
$error .= "You dont have ZIP extension";
}
?>
我在尝试执行我的代码时收到这些警告
Warning: fopen(ZipFIle/) [function.fopen]: failed to open stream: Is a directory in /home/my-server/download.php on line 9
Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/my-server/download.php on line 10
Warning: fclose(): supplied argument is not a valid stream resource in /home/my-server/download.php on line 14
Warning: Cannot modify header information - headers already sent by (output started at /home/my-server/download.php:9) in /home/my-server/download.php on line 44
Warning: Cannot modify header information - headers already sent by (output started at /home/my-server/download.php:9) in /home/my-server/download.php on line 45
敬请指导!
这些消息都是服务器warnings
。浏览器不相关。
我正在尝试创建一个 zip 文件并使用 ZipArchive 库通过 PHP 脚本下载它。
$post['files']
此变量包含 URL 托管在 Amazon S3 上的图像,因此我首先将它们下载到我的服务器,创建 zip 文件并下载它们。
我当前的代码在 chrome 上工作正常 我也在 Safari 和 Opera 上测试过,它工作正常但是当我尝试在 Mozilla Firefox 上做同样的事情时,它现在工作了。
请看看我的php脚本
<?php if(isset($_POST['createZip'])){ $post = $_POST; $file_folder = "ZipFIle"; // folder to load files //downloading on my server foreach($post['files'] as $file){ $ch = curl_init($file); $fp = fopen($file_folder.basename($file), 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); } if(extension_loaded('zip')){ // Checking ZIP extension is available if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected $zip = new ZipArchive(); // Load zip library $zip_name = "ZipFIle.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"; } $dir = new DirectoryIterator("ZipFIle"); foreach ($dir as $fileinfo) { if (!$fileinfo->isDot()) { //var_dump($fileinfo->getFilename()); $imgfile = $fileinfo->getFilename(); foreach($post['files'] as $file){ if ( basename($file) == $imgfile) { //get file and add it to zip $zip->addFile($file_folder.$imgfile);// Adding files into zip } }//foreach($post['files'] as $file) }//if } $zip->close(); if(file_exists($zip_name)){ //echo $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); foreach ($dir as $fileInfo) { if(!$fileInfo->isDot()) { //delete all files after creating zip unlink($fileInfo->getPathname()); } }//foreach for delete }else{echo "no zip";} }else $error .= "Please select file to zip"; }else $error .= "You dont have ZIP extension"; } ?>
我在尝试执行我的代码时收到这些警告
Warning: fopen(ZipFIle/) [function.fopen]: failed to open stream: Is a directory in /home/my-server/download.php on line 9 Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/my-server/download.php on line 10 Warning: fclose(): supplied argument is not a valid stream resource in /home/my-server/download.php on line 14 Warning: Cannot modify header information - headers already sent by (output started at /home/my-server/download.php:9) in /home/my-server/download.php on line 44 Warning: Cannot modify header information - headers already sent by (output started at /home/my-server/download.php:9) in /home/my-server/download.php on line 45
敬请指导!
这些消息都是服务器warnings
。浏览器不相关。