0

Possible Duplicate:
Open file, write to file, save file as a zip and stream to user for download

I have a txt file in uploads folder where i need to add this file and create a ZIP on fly and prompt the user to download the Zip file which should contain the text file .How do i do this and moreover what are the headers needed for prompting the user for download .can any one Help me out. I have used the same constucts from PHP Manual.Iam Using the same function to zip the file "http://davidwalsh.name/create-zip-php" and iam writing this code and iam getting prompted for zip download but iam getting the caution message when i extarct the file

$zip_file = "my-archive.zip";
$file_path="../../../downloads/";
$files_to_zip = array($file_path."test_testing.txt");
$result = create_zip($files_to_zip,'my-archive.zip');

header('Content-Description: File Transfer');    
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zip_file);
header("Pragma: no-cache");
header("Expires: 0");

readfile($zip_file);    

The Caution error message is "one or more files in this archive uses ".."(parent folder) as part of its folder information "

Community
  • 1
  • 1
Someone
  • 10,405
  • 23
  • 67
  • 100
  • possible duplicate of [Open file, write to file, save file as a zip and stream to user for download](http://stackoverflow.com/questions/2286639/open-file-write-to-file-save-file-as-a-zip-and-stream-to-user-for-download) or http://stackoverflow.com/questions/3972342/3972544#3972544 – Mark Baker Oct 21 '10 at 14:29

1 Answers1

4

In the create_zip() function, change

foreach($valid_files as $file) {
    $zip->addFile($file,$file);
}

to

foreach($valid_files as $file) {
    $zip->addFile($file,pathinfo($file,PATHINFO_BASENAME));
}

This will work as long as you don't have files with duplicate names but different directories in your array of files

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks,it worked out But would all the above headers be sufficient for compresssed Zip download.I have a text file of 10 MB and when i zip that it should be compressed up to 250KB or some thing but again it is showing that much amount of time for download .Do we need to add any header – Someone Oct 21 '10 at 15:48
  • @Derby: add `header("Content-Length: ".filesize($zip_file));` – bob-the-destroyer Oct 21 '10 at 21:58
  • Do I have to inlude any files, to provide this script to work? In my case, it doesn't create the ZIP-file and doesn't do anything.. – James Cazzetta Feb 24 '12 at 16:44
  • Have you included the code from http://davidwalsh.name/create-zip-php – Mark Baker Feb 24 '12 at 16:49