1
votes

I am adding files to Zip with php using the following php script.

<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
session_start();

$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name,  ZipArchive::CREATE);

$filesinSession = $_SESSION['files'];
$filesToAdd = explode(';', $filesinSession);
for($i = 1 ; $i <= sizeof($filesToAdd)-1 ; $i++)
{
    //echo $filesToAdd[$i]."<br>";
    $zip->addFile($filesToAdd[$i]);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

header('Content-type: application/zip');
header('Content-disposition: filename="' . $zip_name . '"');
header("Content-length: " . filesize($zip_name));
$zip_file = file_get_contents($zip_name);
readfile($zip_name);
exit();

?>

note:session contains file name semicolon separated.

the file generated as a result of above code execution. is damaged when i open that file in sublime text. it shows following error.

///////////////////zip opened in sublime text///////////////////////////////////
Warning: filesize(): stat failed for 1508220713.zip in C:\xampp\htdocs\HMApp1\download.php on line 21

Warning: file_get_contents(1508220713.zip): failed to open stream: No such file or directory in C:\xampp\htdocs\HMApp1\download.php on line 22

Warning: readfile(1508220713.zip): failed to open stream: No such file or directory in C:\xampp\htdocs\HMApp1\download.php on line 23

1
have you enought permissions to create file¿?Álvaro Touzón
@ÁlvaroTouzón the zip file is created in the same folder where my script lies. But when I try to read that zip for download it throws above three errors.Qasim Nadeem
then seems php is not localize your .zip file,Álvaro Touzón
@ÁlvaroTouzón so what should I do?Qasim Nadeem
have u called $zip->close(); ?tan

1 Answers

2
votes

I found some classic mistakes in your code, here they are

suppose your session contain files which present in same directory relatively

$filesinSession = 'gallery/1/t.jpg;gallery/1/t2.jpg;'; // if ending with ;

add trim $filesToAdd = explode(';', trim($filesinSession, ';'));

use 0 instead 1

for ($i = 0; $i <= sizeof($filesToAdd) - 1; $i++) {

add local file name during add // best practice

$zip->addFile($filesToAdd[$i], basename($filesToAdd[$i]));

don't forgot to close

$zip->close();

fixed header

header('Content-Disposition: attachment; filename="' . $zip_name . '"');

removed file_get_contents you don't need it

clean buffer // escape server error

ob_end_clean();
flush();

Full Source

$zip = new ZipArchive();
$zip_name = time() . ".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);

$filesinSession = 'gallery/1/t.jpg;gallery/1/t2.jpg;';
$filesToAdd = explode(';', trim($filesinSession, ';'));
for ($i = 0; $i <= sizeof($filesToAdd) - 1; $i++) {
    $zip->addFile($filesToAdd[$i], basename($filesToAdd[$i]));
}
$zip->close();
$size = filesize($filename);
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $zip_name . '"');
header("Content-length: " . filesize($zip_name));
ob_end_clean();
flush();
readfile($zip_name);
exit();