Page 1 of 1

Zip

Posted: Fri Apr 27, 2007 5:51 am
by kpraman
How i can zip folders? I searched in google, i am only getting zipping files.

Posted: Fri Apr 27, 2007 8:01 am
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.

Posted: Fri Apr 27, 2007 10:15 am
by Begby
Its not working how? Do you get any errors?

Posted: Fri Apr 27, 2007 10:49 am
by kpraman
No. but, i am not getting the files, its blank

Posted: Fri Apr 27, 2007 11:43 am
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.

Posted: Sat Apr 28, 2007 12:27 am
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

Posted: Sat Apr 28, 2007 2:35 pm
by feyd
Backtick strings directly execute via system().