How to ZIP files with PHP on Dreamhost

How to ZIP files with PHP on Dreamhost

Posted by: simplemedia
Posted on: 2008-11-14 10:28:00

Hi all,

I am writing a PHP script that builds a new folder and fills it with files. Now I want to add a feature where the script zips up the folder and offers a link for download, on demand.

So I'm wondering how to get PHP to zip the files up. I've googled and asked around, and it seems like there's multiple solutions/plugins, but I'm not sure what the best option would be - especially for Dreamhost. Does Dreamhost have a PHP ZIP module of some sort already loaded?

Any help on this is much appreciated!

Re: How to ZIP files with PHP on Dreamhost

Posted by: patricktan
Posted on: 2008-11-14 14:16:00

Have you read this?
http://au.php.net/manual/en/book.zip.php

Try the ziparchive function.

Re: How to ZIP files with PHP on Dreamhost

Posted by: sXi
Posted on: 2008-11-15 03:48:00

Compile PHP with zip support using zlib-1.2.3




How To Install PHP.INI / ionCube on DreamHost

Re: How to ZIP files with PHP on Dreamhost

Posted by: Dahak
Posted on: 2008-11-16 17:54:00

This is what I use on my own site. My script pulls out a filtered list of file extensions from a directory.

First, you'll need to include zip.lib.php (here's the version I'm using: http://thefifthimperium.com/Random/zip.lib.php.txt, just save that file as zip.lib.php).

Below is the business end of the script I use to do the collection:

Obvious things you need to define - $zipdir, $ad_dir and $logdir, if you want it.

$suffix_array is a list of file extensions that save to the zip file:

$suffix_array = array ( '.htm', '.html', '.jpg', '.gif', '.opf' );

$about_message is just a variable containing, in my case, an 'About' message describing the collection. I build a small, but complete, HTML page in the variable and save it first to the zip file with a name that has a fairly good chance of listing first in the archive.


    if ( !file_exists( $zipdir ) ) {
mkdir( $zipdir ); // Make ZIP storage directory, if needed
}
if ( !file_exists( $logdir ) ) { // if you intend to log things
mkdir( $logdir ); // Make log directory, if needed
}

if ( !file_exists( $zipdir . '/' . $zipfilename ) ) {
$zipfile = new zipfile(); // form is posted, handle it
$zipfile ->addFile( stripcslashes( $about_message ), $zip_subfolder . '/_about_this_file.htm' );
if ( $handle = opendir( $ad_dir ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if (!is_dir( $file ) && $file != "." && $file != "..") {
foreach ( $suffix_array as $suffix ) {
$len = strlen( $suffix );
if ( strtolower( substr( $file, -$len, $len ) == $suffix ) ) {

$f_tmp = @fopen( $ad_dir . '/' . $file, 'r');
if($f_tmp){
$dump_buffer=fread( $f_tmp, filesize( $ad_dir . '/' . $file ) );
$zipfile -> addFile( $dump_buffer, $zip_subfolder . '/' . $file );
fclose( $f_tmp );
}
}
}
}
}
$dump_buffer = $zipfile -> file();
}

// write the file to disk:
$file_pointer = fopen( $zipdir . '/' . $zipfilename, 'w' );
if( $file_pointer ){
fwrite( $file_pointer, $dump_buffer, strlen($dump_buffer) );
fclose( $file_pointer );
}
}

// log it, you can delete this bit, if you want to
$file_pointer = fopen( $logdir . '/' . $logname, 'a' );
if( $file_pointer ){
$logstr = '"' . $book . '","' . $format . '","' . $cd_name . '","' . $datestr . '","' . date('Y-m-d H:i:s.0T') . '","' . $_SERVER['REMOTE_ADDR'] . "\"\n";
fwrite( $file_pointer, $logstr );
fclose( $file_pointer );
}

My own script then feeds the new (or pre-existing) zip file out to the browser, so I continue on with this:

  // response zip archive to browser:
header('Pragma: public');
header('Content-type: application/zip');
header('Content-length: ' . filesize($zipdir . '/' . $zipfilename));
header('Content-Disposition: attachment; filename="'.$zipfilename.'"');

readfile( $zipdir . '/' . $zipfilename );

htmlstartpage( $d_name, $badge );

echo <<<EOT


<center>
Thank you for downloading the <em>$format</em> version of <em>$book</em> in the file <em>$zipfilename</em>.

</center>
</body>
</html>
EOT;

exit;

Re: How to ZIP files with PHP on Dreamhost

Posted by: simplemedia
Posted on: 2008-11-17 15:37:00

This was great, very helpful.

One question - sorry cuz I'm still learning with PHP - how do I "include zip.lib.php" ?

Thanks again!

Re: How to ZIP files with PHP on Dreamhost

Posted by: patricktan
Posted on: 2008-11-19 01:02:00

include_once('yourpath/zip.lib.php');

adding zip extensions to DH custom php build

Posted by: net-buoy
Posted on: 2008-12-07 13:52:00

I need to build php with sockets, xmlrpc and zip support. This will have to vecome a stnadard build for Moodle users and I want to try and make this as comprehensive a solution as possible.

The install/prep scripts include support for only sockets (i.e. not xmlrpc and zip) and the notes about lines to add are a bit inconsistent..... Do you have a version of the prep and install script to build a custom DH php 5.6.3 with these extensions? I am particulary concerned about source and install directories as well as with the use of with vs enable with the zip option....

Re: adding zip extensions to DH custom php build

Posted by: sXi
Posted on: 2008-12-10 18:24:00

Syntax is --enable-zip & --enable-xmlrpc




How To Install PHP.INI / ionCube on DreamHost

Re: adding zip extensions to DH custom php build

Posted by: net-buoy
Posted on: 2008-12-11 10:04:00

That's where I was going but I am not sure how to set the script so it sources the dependencies without pointing to a directory.

Re: adding zip extensions to DH custom php build

Posted by: sXi
Posted on: 2008-12-11 10:50:00

Assuming your sources are in ${SOMEDIR}
(ie. where the bin, etc, lib, include directories are being created by whatever script you're using).

get zlib, extract zlib

cd [zlib_directory]
./configure --shared --prefix=${SOMEDIR}
nice -n 19 make
make install

Then add the previously mentioned --enable's to your php build instructions.




How To Install PHP.INI / ionCube on DreamHost

Re: adding zip extensions to DH custom php build

Posted by: net-buoy
Posted on: 2008-12-11 19:06:00

The reference below is what is confusing in that it suggests --with-zip=DIR
http://www.w3schools.com/php/php_ref_zip.asp

have you used the 0.13.49 zziplib-preview? is this really development code?



Re: adding zip extensions to DH custom php build

Posted by: sXi
Posted on: 2008-12-12 13:24:00

PHP 5.2+ uses zlib functions.

ZIP Manual at PHP.NET




How To Install PHP.INI / ionCube on DreamHost

Tags: new folderzipsphp script