Hi,
I am trying to download multiple files using a zip file.
I want the php script to form a zip file after adding all the file from the folder that I want, then download that zip file.
I added an unlink function to make my zip file disappear, so the zip file downloaded will always be the one inside the folder.
Right now, by commenting the unlink, I can know that the zip folder is created inside the server. However there is nothing inside.
The downloaded zip file when I open, I see the message, the zip file folder is invalid. windows can't open the file ( I use a WAMP server). However, if I use 7zip to extract it, I can obtain and see the file, which is very confusing.
What am I missing? Is it the header?
echo"<form action='' method=POST enctype=multipart/form-data>";
echo"<input type=submit value='Export' name=downloadaszip class=downloadaszip>";
echo"</form>";
if(isset($_POST['downloadaszip']))
{
//Directory
$files=glob('.\\'.$_SESSION['folder'].'\cfile\*');
$zip = new ZipArchive();
//Open a ZIP file archive
$zip->open('./user_'.$_SESSION['membername'].'/project.zip', ZipArchive::CREATE);
//add each file to archive
foreach ($files as $file)
{
//Adds a file to a ZIP archive from the given path
$zip->addFile($file);
}
//Close the active archive
$zip->close();
//identify browser type of file downloaded
header('Content-Type: application/zip');
//to produce download box and prompt user with project.zip
header('Content-disposition: attachment; filename=project.zip');
//download
header("Pragma: no-cache");
header("Expires: 0");
readfile("./user_".$_SESSION['membername']."/project.zip");
//unlink('./'.$_SESSION['folder'].'/project.zip');
}
Thanks in advance.