Topic: Creating a zip file to download (Read 2212 times)
Atari ST fan
Posts: 8
52 credits Members referred : 0
« on: Jul 08, 2009, 06:36:26 am »
i hope someone can tell me how to fix my code. ive been trying on 4 different sites to get some feedback on what is wrong. everyone keeps sending me to the manual. ive read the manual it doesnt tell me whats wrong with my code. it only tells me how to write it.
ok heres what im trying to do. i want to be able to input a username into a text box. take that variable and use it to zip the corresponding folder and download to my local machine. right now my script does almost what i want with 2 exceptions. 1. it saves the zipped file back to the server, it wont allow me to save the file to the local machine. 2. it only adds the files from the folder that i hard code into the script. this is the code for it.
/* creates a compressed zip file */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close();
//check to make sure the file exists return file_exists($destination); } else { return false; } } $buffer = file_get_contents($file);
/* Set data type, size and filename */ header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . strlen($buffer)); header("Content-Disposition: attachment; filename=$filename.zip");
/* Send our file... */ echo $buffer;
?>
i have several different scripts the one i mentioned above ive been working with most because it is so close to what i want. this is another one that opens the dialog box but it doesnt actually seem to add any files to the zip folder. the code below is a one of the variations of the one above.
Code:
<?php
function get_files_from_folder($directory, $put_into) { if ($handle = opendir('upload_test/' . $_POST['userName'])) { echo "Directory handle: $handle/n"; echo "Files:/n";
/* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if (is_file($directory.$file)) { $fileContents = file_get_contents($directory.$file); $this->addFile($fileContents, $put_into.$file); } elseif ($file != 'ajay' and $file != 'vibha' and is_dir($directory.$file)) { $this->addDirectory($put_into.$file.'/'); $this->get_files_from_folder($directory.$file.'/', $put_into.$file.'/'); }//close elseif echo "$file/n"; }//close while
/* This is the WRONG way to loop over the directory. while ($file = readdir($handle)) { echo "$file/n"; }*/ }//close if closedir($handle); }//close function
//if true, good; if false, zip creation failed $result = create_zip($files_to_zip,'test.zip');
/* creates a compressed zip file */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done! $zip->close();
//check to make sure the file exists return file_exists($destination); } else { return false; } } $destination = file_get_contents($result);
/* Set data type, size and filename */ header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . strlen($destination)); header("Content-Disposition: attachment; filename=$filename.zip");
/* Send our file... */ echo $destination;
?>
both of the scripts create a zip file that when you try to open it says "The file is corrupt or invalid", and the file is something like 500 bytes instead of the larger zip file i get on the server thats like 105KB. which leads me to believe that it is not putting anything into the zip files but am unsure why.
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #2 on: Jul 08, 2009, 08:01:58 pm »
not entirely sure where the top part came from. i am working on this with someone and he provided that portion of the code on the second snippet. the other parts of it have been put together from several locations on the web. i know i need the headers, because they pop up the dialog box for me to save it where i want. the one thing im not sure about is how do i put items into the array without knowing how many there are. so was trying either to use a for loop or a while loop to loop through the folder and add each image to the zip folder. but that doesnt seem to be working.
ive actually cut and pasted several of them together, and edited them to my needs. i realize that part of it seems to have someones name in it. i dont really need that part and i suppose it can be cut out, i just didnt notice it before i pasted it here.
« Last Edit: Jul 08, 2009, 08:07:19 pm by neekworld »
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #4 on: Jul 08, 2009, 08:09:54 pm »
yes i think that is where i started. but the top one actually creates the zip file but it saves it to the server not the local machine. which is what i want. i was trying to edit it so that i could get it to do what the top one was but to save it to the local machine. im using a WAMP as my server set up
« Last Edit: Jul 08, 2009, 08:12:58 pm by neekworld »
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #5 on: Jul 08, 2009, 08:17:45 pm »
Maybe the WAMP server is the problem.
I use this code for most of the time (the zip creation is different than yours)
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #6 on: Jul 08, 2009, 08:59:06 pm »
so i should try and replace my header portion with what you put here?
i dont think its the WAMP server ive never had problems with it. the headers im using pop up the dialog box but they dont save it to the file name that is put in the other portion of the code. i think the main problem is i put 2 scripts together to see if they would work.
i actually got an idea from another post. they suggested i create the zip folder then put a link to download the zipped folder from the server. still working on it because it cant seem to find the zipped folder right now. have to figure out how to word the link so that it can see the file.
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #7 on: Jul 08, 2009, 09:07:21 pm »
I think there is also a zip function via the dos commandline...
but you're right headers one windows could be different from Linux do you checked this on google too? (I"m sure there are more windows user with that problem)
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #8 on: Jul 08, 2009, 09:20:38 pm »
actually no. i have been searching for a week to through google. no one is trying to do what i am. i might try your headers just to see if it works. the other site ive posted on said i need to get a list of the files in the folder that i want to zip. then use that list to stuff the files into the zip folder i create. then open the dialog box to force the download. im not bad with the php but this one seems to be eluding me. do you have any suggestions? i think your site refers to using createZip. when i try to use just your code without modifying it it tells me that createZip doesnt exist or something. on the WAMP i have to check the extensions. i have the zip one on but it still doesnt see the createZip.
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #9 on: Jul 09, 2009, 07:00:37 am »
CreateZip is a 3rd party class check the tutorial.
You can use that class to include the files using the code from the tutorial. the zip command line tools are not a php extension but the buildin zip function from newer windows versions
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #10 on: Jul 15, 2009, 07:54:09 pm »
i have tried using the CreateZip but it gives me an error saying it doesnt exist or something. which tutorial are you referring to? i have looked at so many sites trying to resolve this issue i may have seen it but i dont remember what page its on.
/* creates a compressed zip file */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close(); //check to make sure the file exists return file_exists($destination); } else { return false; } } $buffer = file_get_contents($file);
/* Set data type, size and filename */ header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . strlen($buffer)); header("Content-Disposition: attachment; filename=$filename.zip");
/* Send our file... */ echo $buffer;
?>
i was thinking of putting something like this in there to browse through the directory and add each file to the array but was not sure how to modify my code and insert it into the zip file as the tutorial i have doesnt exactly show how to add images to the array only lines from a text file.
Code:
$dir = dir('path to file'); if($dir === FALSE) throw new NoSuchDirectoryException(); while (($entry = @$dir->read()) !== FALSE) { echo $entry; }
this code only echos the entry but i want to add it to the files_to_zip variable. so i would need to put this in the while statement right?
Code:
//add the files foreach($valid_files as $file) { $zip->addFile($file,$file); }
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #11 on: Jul 15, 2009, 10:05:04 pm »
read the thread the link was posted before (and read the tutorial)
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #12 on: Jul 17, 2009, 06:01:24 am »
are you talking about this one web-development-blog.com/archives/tutorial-create-a-zip-file-from-folders-on-the-fly/
because if you are i tried that exact script but it gives me an error that the createZip does not exist. i have the zip extensions turned on, on my server too.
also where do the last two sections of code go in the main php script below it or in it somewhere? because no one has the entire script put together. and when i try to piece it together i get multiple errors. i can fix some of them but when i get that the main class (ie createZip) doesnt exits that kills the entire script, defeating the purpose of the thing.
i have different people directing me to different tutorials, but none of them do exactly what i want. they either create a zip, which i can do; but that zip file is saved back to the server. or they pop up the dialog box that allows you to download the zip. none of them do both at the same time. i have tried doing two separate ones too, first one to create the zip on the server, second to download it. it creates the zip file fine but when i click on the download link it says it cant find the zip file i created.
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #13 on: Jul 17, 2009, 07:06:56 am »
right in the beginning there is a sentence:
"A short search on Google has lead me to the Create ZIP File PHP class from Rochak Chauhan."
why should I help if people didn't read that artilce
Atari ST fan
Posts: 8
52 credits Members referred : 0
« Reply #14 on: Jul 22, 2009, 10:16:12 pm »
um when i went to that site i put in the last post, there is no link to Create ZIP File PHP class from Rochak Chauhan website. i think there was at one time but when i tried it it just reloaded the same page. hence the reason i was asking.
« Last Edit: Jul 22, 2009, 10:28:52 pm by neekworld »
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
um when i went to that site i put in the last post, there is no link to Create ZIP File PHP class from Rochak Chauhan website. i think there was at one time but when i tried it it just reloaded the same page. hence the reason i was asking.