9, February 2010

Creating a zip file to download - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Creating a zip file to download
« previous next »
Pages: [1] Print

Author Topic: Creating a zip file to download  (Read 949 times)
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« on: Jul 08, 2009, 07: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.

Code:
<?php
$files_to_zip 
= array(
    
'upload_test/user/2009-07-01-214005Logo.jpg',
    
'upload_test/user/2009-07-01-215314orange_bg.gif',
    
'upload_test/user/2009-07-01-215326emptycart.gif',
    
'upload_test/user/2009-07-01-223317dash.gif',
    
'upload_test/user/2009-07-02-020536arrow.gif',
    
'upload_test/user/2009-07-02-020717check_green.gif'
);
//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;
    }
}
  
$buffer file_get_contents($file);

 
/* Force download dialog... */
    
header("Content-Type: application/force-download");
    
header("Content-Type: application/octet-stream");
    
header("Content-Type: application/download");

    
/* Don't allow caching... */
    
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    
/* 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);

/* Force download dialog... */
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

/* Don't allow caching... */
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

/* 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: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jul 08, 2009, 09:21:17 am »

Hi,

where do you found the second snippet?


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #2 on: Jul 08, 2009, 09: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, 09:07:19 pm by neekworld »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jul 08, 2009, 09:04:16 pm »

looks like some of my code:

check this tutorial:
http://www.web-development-blog.com/archives/tutorial-create-a-zip-file-from-folders-on-the-fly/

I use by myself the zip function from Linux


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #4 on: Jul 08, 2009, 09: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, 09:12:58 pm by neekworld »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jul 08, 2009, 09: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)

$zip $filestr['filename'].'.'.$filestr['extension'];
	
	
	
	

	
	
	
	
exec(sprintf('zip -r %s %s'$zip$filestr['filename'].'/'));
	
	
	
	

	
	
	
	
header("Pragma: public");
	
	
	
	
header("Expires: 0");
	
	
	
	
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	
	
	
	
header("Cache-Control: private"false);
	
	
	
	
header("Content-Type: application/zip");
	
	
	
	
header("Content-Disposition: attachment; filename=".basename($zip).";" );
	
	
	
	
header("Content-Transfer-Encoding: binary");
	
	
	
	
header("Content-Length: ".filesize($zip));
	
	
	
	
readfile($zip);
	
	
	
	
@
unlink($zip);
	
	
	
	
exit;
?>


The zip class has some problems in some browsers (check the comment from the blog)


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #6 on: Jul 08, 2009, 09: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: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jul 08, 2009, 10: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)


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #8 on: Jul 08, 2009, 10: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: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Jul 09, 2009, 08: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


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #10 on: Jul 15, 2009, 08: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.

here is the code im using now.

Code:
<?php
$files_to_zip 
= array(
 
   'upload_test/user/2009-07-01-214005Logo.jpg',
 
   'upload_test/user/2009-07-01-215314orange_bg.gif',
 
   'upload_test/user/2009-07-01-215326emptycart.gif',
 
   'upload_test/user/2009-07-01-223317dash.gif',
 
   'upload_test/user/2009-07-02-020536arrow.gif',
 
   'upload_test/user/2009-07-02-020717check_green.gif'
);
//if true, good; if false, zip creation failed
$result create_zip($files_to_zip,$_POST['userName'].'.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;
 
   }
}
 
 $buffer file_get_contents($file);

 
/* Force download dialog... */
 
   header("Content-Type: application/force-download");
 
   header("Content-Type: application/octet-stream");
 
   header("Content-Type: application/download");

 
   /* Don't allow caching... */
 
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

 
   /* 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: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Jul 15, 2009, 11:05:04 pm »

read the thread the link was posted before (and read the tutorial)


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #12 on: Jul 17, 2009, 07: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: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Jul 17, 2009, 08: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 Cheesy


Last blog : A new Wordpress theme for our blog
Atari ST fan
*
Posts: 8
52 credits
Members referred : 0



« Reply #14 on: Jul 22, 2009, 11: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, 11:28:52 pm by neekworld »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6690
34708 credits
Members referred : 374


It's time to use PHP5!


« Reply #15 on: Jul 23, 2009, 07:38:35 am »

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.

There is no link in that tutorial?


Last blog : A new Wordpress theme for our blog
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=8905
Tags : zip php Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Creating a zip file to download
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Feb 09, 2010, 10:09:31 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2010 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.