How to add a txt file and create a ZIP in php [duplicate]
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-archi开发者_JAVA百科ve.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 "
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
精彩评论