How to add txt file and create zip in php

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

I have a txt file in the uploads folder, where I need to add this file and create a ZIP on the fly, and ask the user to upload a Zip file, which should contain a text file. How do I do this, and especially the headers needed to request the user to download. can any one Help me out. I used the same constants from PHP Manual.Iam Using the same function for the zip file " http://davidwalsh.name/create-zip-php ", and I write this code and iam receives a request to download zip, but iam receives warning when i extarct 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); 

Error message: "one or more files in this archive use" .. "(the parent folder) as part of the folder information"

0
php
Oct 21 '10 at 14:25
source share
1 answer

In 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 until you have files with duplicate names, but different directories in your file array

+4
Oct. 21 2018-10-21
source share



All Articles