Php with Zip file Problem

I am using the the following code to download files from server.

<?php 

// Class definition found at http://www.zend.com/zend/spotlight/creating-zip-files3.php
// Some alterations to the original posted code were made in order to get everything working properly
// See example usage at the bottom of this page

class zipfile
{
var $datasec = array();
var $ctrl_dir = array();
var $eof_ctrl_dir = "\\x50\\x4b\\x05\\x06\\x00\\x00\\x00\\x00";
var $old_offset = 0; 

function add_dir($name) {
$name = str_replace("", "/", $name); 

$fr = "\\x50\\x4b\\x03\\x04";
$fr .= "\\x0a\\x00";
$fr .= "\\x00\\x00";
$fr .= "\\x00\\x00";
$fr .= "\\x00\\x00\\x00\\x00"; 

$fr .= pack("V",0);
$fr .= pack("V",0);
$fr .= pack("V",0);
$fr .= pack("v", strlen($name) );
$fr .= pack("v", 0 );
$fr .= $name;
$fr .= pack("V", 0);
$fr .= pack("V", 0);
$fr .= pack("V", 0); 

$this -> datasec[] = $fr;
$new_offset = strlen(implode("", $this->datasec)); 

$cdrec = "\\x50\\x4b\\x01\\x02";
$cdrec .="\\x00\\x00";
$cdrec .="\\x0a\\x00";
$cdrec .="\\x00\\x00";
$cdrec .="\\x00\\x00";
$cdrec .="\\x00\\x00\\x00\\x00";
$cdrec .= pack("V",0);
$cdrec .= pack("V",0);
$cdrec .= pack("V",0);
$cdrec .= pack("v", strlen($name) );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$ext = "\\x00\\x00\\x10\\x00";
$ext = "\\xff\\xff\\xff\\xff";
$cdrec .= pack("V", 16 );
$cdrec .= pack("V", $this -> old_offset );
$cdrec .= $name; 

$this -> ctrl_dir[] = $cdrec;
$this -> old_offset = $new_offset;
return;
} 

function add_file($data, $name) {
$fp = fopen($data,"r");
$data = fread($fp,filesize($data));
fclose($fp);
$name = str_replace("", "/", $name);
$unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data);
$zdata = substr ($zdata, 2, -4);
$c_len = strlen($zdata);
$fr = "\\x50\\x4b\\x03\\x04";
$fr .= "\\x14\\x00";
$fr .= "\\x00\\x00";
$fr .= "\\x08\\x00";
$fr .= "\\x00\\x00\\x00\\x00";
$fr .= pack("V",$crc);
$fr .= pack("V",$c_len);
$fr .= pack("V",$unc_len);
$fr .= pack("v", strlen($name) );
$fr .= pack("v", 0 );
$fr .= $name;
$fr .= $zdata;
$fr .= pack("V",$crc);
$fr .= pack("V",$c_len);
$fr .= pack("V",$unc_len); 

$this -> datasec[] = $fr;
$new_offset = strlen(implode("", $this->datasec)); 

$cdrec = "\\x50\\x4b\\x01\\x02";
$cdrec .="\\x00\\x00";
$cdrec .="\\x14\\x00";
$cdrec .="\\x00\\x00";
$cdrec .="\\x08\\x00";
$cdrec .="\\x00\\x00\\x00\\x00";
$cdrec .= pack("V",$crc);
$cdrec .= pack("V",$c_len);
$cdrec .= pack("V",$unc_len);
$cdrec .= pack("v", strlen($name) );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("v", 0 );
$cdrec .= pack("V", 32 );
$cdrec .= pack("V", $this -> old_offset ); 

$this -> old_offset = $new_offset; 

$cdrec .= $name;
$this -> ctrl_dir[] = $cdrec;
} 

function file() {
$data = implode("", $this -> datasec);
$ctrldir = implode("", $this -> ctrl_dir); 
	
return
$data .
$ctrldir .
$this -> eof_ctrl_dir .
pack("v", sizeof($this -> ctrl_dir)) .
pack("v", sizeof($this -> ctrl_dir)) .
pack("V", strlen($ctrldir)) .
pack("V", strlen($data)) .
"\\x00\\x00";
}
}

// Test this class
$zipTest = new zipfile();
$zipTest->add_dir("thumnails/");
$file="Sample.wma";
$zipTest->add_file("sample/".$file,"images/sample.wma");
$zipTest->add_file("images/2.jpg", "images/2.jpg");

// Return Zip File to Browser
Header("Content-type: application/octet-stream");
Header ("Content-disposition: attachment; filename=zipTest.zip");
echo $zipTest->file();
?>

Its working nice. But I am facing 2 problem with this code
1.While im downloading from localhost , Download ll be stopped after 25 MB
2.While im downloading from server , Download ll be stopped after 7.2MB

Some One knows it Please clarify …

I’m guessing the script times out.
I.e. it takes for example 20 seconds to create the zip file, and then 10 seconds to echo this file.
Since your local network has more bandwith than the remote internet connection, you can receive more data in 10 seconds when testing locally.

A better way would be to create the zip file, redirect, and then send the zip file.
Also, only create the zip file if it’s not already there. Saves a lot of redudant work!

thanks for reply.

But where should i redirect?

Is any other way available to change the settings ? (ie. increase the timing.)

Basically that setup would work like


if (!isset($_GET['download'])) {
  // if zip file does not yet exist {
    // create ZIP file, and save it on disk
  // }
  header('Location: '.$_SERVER['PHP_SELF'].'?download=1');
} else {
  // send the ZIP file to the user
}

And then you send the user to this script without any GET parameters.
The script will then create the ZIP file if it does not already exists, and then redirect to itself with the GET parameter ?download=1, which will then send the ZIP to the user.

And yes, you could use set_time_limit, but I wouldn’t advise you to do so …