Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a zip file and download it

Tags:

php

download

zip

I am trying to download a 2 files by creating the zip file on local-server.the file is downloaded in zip format but when i try to extract it.it gives error: End-of-central-directory signature not found. Either this file is not a zip file, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zip file comment will be found on the last disk(s) of this archive.

the following code i am using for this:

 <?php $file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');  //Archive name $archive_file_name=$name.'iMUST_Products.zip';  //Download Files path $file_path=$_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';   zipFilesAndDownload($file_names,$archive_file_name,$file_path);  function zipFilesAndDownload($file_names,$archive_file_name,$file_path) {         //echo $file_path;die;     $zip = new ZipArchive();     //create the file and throw the error if unsuccessful     if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {         exit("cannot open <$archive_file_name>\n");     }     //add each files of $file_name array to archive     foreach($file_names as $files)     {         $zip->addFile($file_path.$files,$files);         //echo $file_path.$files,$files."      }     $zip->close();     //then send the headers to force download the zip file     header("Content-type: application/zip");      header("Content-Disposition: attachment; filename=$archive_file_name");      header("Pragma: no-cache");      header("Expires: 0");      readfile("$archive_file_name");     exit; }     ?> 

i checked the values of all variables which are passing into the function,all are fine.so please look this.Thanks in advance.

like image 396
Harshal Mahajan Avatar asked Sep 01 '12 07:09

Harshal Mahajan


People also ask

Can Windows 10 create Zip files?

Zipping is one of the oldest and most commonly used methods for compressing files. It's used to save space and share big files quickly. In the past, you needed third-party programs like WinZip to unzip files in Windows. But Windows 10 lets you zip and unzip any file you want just by right-clicking.

Where can I create a ZIP file?

ezyZip offers various options to manipulate zip files including zipping individual files, create a zipped folder, extracting zip files or converting zip files.


1 Answers

Add Content-length header describing size of zip file in bytes.

header("Content-type: application/zip");  header("Content-Disposition: attachment; filename=$archive_file_name"); header("Content-length: " . filesize($archive_file_name)); header("Pragma: no-cache");  header("Expires: 0");  readfile("$archive_file_name"); 

Also make sure that there is absolutely no white space before <? and after ?>. I see a space here:

 <?php $file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf'); 
like image 131
Kuba Wyrostek Avatar answered Sep 21 '22 04:09

Kuba Wyrostek