Zip

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Zip

Post by kpraman »

How i can zip folders? I searched in google, i am only getting zipping files.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

This code is not working for me.

Code: Select all

<?PHP

// this script zips up a directory (on the fly) and delivers it
// C.Kaiser 2002
// No Copyright, free to use.

  // get the filename of this php file without extension.
  // that is also the directory to zip and the name of the
  // zip file to deliver
  $filename_no_ext=str_replace(".php","",basename($SCRIPT_NAME));

  // we deliver a zip file
  header("Content-Type: archive/zip");

  // filename for the browser to save the zip file
  header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");

  // get a tmp name for the .zip
  $tmp_zip = tempnam ("tmp", "tempname") . ".zip";

  // zip the stuff (dir and all in there) into the tmp_zip file
  `zip -r $tmp_zip $filename_no_ext`;
 
  // calc the length of the zip. it is needed for the progress bar of the browser
  $filesize = filesize($tmp_zip);
  header("Content-Length: $filesize");

  // deliver the zip file
  $fp = fopen("$tmp_zip","r");
  echo fpassthru($fp);

  // clean up the tmp zip file
  `rm $tmp_zip `;
?>
I use windows.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Its not working how? Do you get any errors?
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

No. but, i am not getting the files, its blank
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You have parse errors,

Code: Select all

// zip the stuff (dir and all in there) into the tmp_zip file
  `zip -r $tmp_zip $filename_no_ext`;
You need to pass this line through exec(), although you may want to pass those variables through escapeshellarg() to escape any nasty characters.
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

I have changed the code to

Code: Select all

$filename_no_ext=str_replace(".php","",basename($SCRIPT_NAME));

  // we deliver a zip file
  header("Content-Type: archive/zip");

  // filename for the browser to save the zip file
  header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");

  // get a tmp name for the .zip
  $tmp_zip = tempnam ("tmp", "tempname") . ".zip";

  // zip the stuff (dir and all in there) into the tmp_zip file
  exec(`zip -r $tmp_zip $filename_no_ext`);
  // calc the length of the zip. it is needed for the progress bar of the browser
  $filesize = filesize($tmp_zip);
  header("Content-Length: $filesize");
Error:
The archive is unknown format or damaged
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Backtick strings directly execute via system().
Post Reply