How to ZIP files with PHP on Dreamhost

software development

#1

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!


#2

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

Try the ziparchive function.

$50 off and 3 free domains with code: [color=#CC0000]DH3[/color] Sign Up NOW or More Codes Here


#3

Compile PHP with zip support using zlib-1.2.3

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#4

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.

[code] 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 );
}
[/code]My own script then feeds the new (or pre-existing) zip file out to the browser, so I continue on with this:

[code] // 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

Thank you for downloading the $format version of $book in the file $zipfilename. EOT;

exit;
[/code]


#5

This was great, very helpful.

One question - sorry cuz I’m still learning with PHP - how do I “include zip.lib.php” ?

Thanks again!


#6

include_once(‘yourpath/zip.lib.php’);

$50 off and 3 free domains with code: [color=#CC0000]DH3[/color] Sign Up NOW or More Codes Here


#7

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…


#8

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

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#9

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.


#10

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.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#11

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?


#12

PHP 5.2+ uses zlib functions.

ZIP Manual at PHP.NET

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost