tags:

views:

44

answers:

1

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 "

+1  A: 

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
@MArk: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
Derby
@Derby: add `header("Content-Length: ".filesize($zip_file));`
bob-the-destroyer