Monday, 25 July 2016

Creating zip and download in php not working in Mozilla Browser



I am trying to create a zip file and download it by PHP script using ZipArchive library.



$post['files'] This variable contains URL of the images which are hosted on Amazon S3 so first I am downloading them to my server, creating zip file and download them.



My current code is working fine with the chrome also i tested on Safari and Opera, its working fine but when I am trying to do the same thing with Mozilla Firefox, its now working.




Please have a look at my php script




<?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";
}

?>


I am getting these warnings when I am trying to execute my code




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


Kindly, guide!


Answer



These messages are all server warnings. The browser is not relevant.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...