Back on this thread....... I have tried using the zlib functions, and the php zip functions, but the best seems to use the shell to the system zip functions. However, I still run out of memory sometimes, or all the time if the user isn't careful. Most of the files are 3.6MB to 485.2MB, so if a user selects more than a couple of files, everything hangs. There must be some way to manage this by zipping in chunks, maybe. I need some advice from someone who has solved this problem. Right now, I'm doing this (the working part of the function):
Code:
chdir($dir); // Change current working directory, so that the zip file gets created in the temp dir
$zfiles = directoryToArray("./", true); // Get contents of the temp directory before we create our zip file, recursive
switch($type) // Add other compression methods?
{
case 'zip': $name .= '.zip'; $z = 1; break;
case 'tar': $name .= '.tar.gz'; $z = 2; break;
default: $name .= '.zip'; $z = 1;
}
foreach($zfiles as $zfile => $timestamp){
list($zfpath,$zfname,$ftotal) = stripPath('./', $zfile);
if($z == 1) {
$output = `zip -r $name $zftotal $zfpath`; // add something to show error number
} else {
if(file_exists($name)) {
$output = `tar -rvz $name $zfpath`;
} else {
$output = `tar -cvz $name $zfpath`;
}
}
}
$testresult = ob_get_contents(); // debug line:
ob_end_clean(); // debug line:
fwrite($fh, $testresult); // debug line:
fclose($fh); // debug line:
return $dir . $name;
The name of the temp archive is passed to this function and pushed to the web user:
Code:
function f_download($file_name) {
global $myFile, $user;
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } // required for IE
// $file_name = str_replace(array("/",".."),"",$file_name);
switch(strtolower(substr(strrchr($file_name,'.'),1)))
{
case 'pdf': $mime = 'application/pdf'; break;
case 'hdf': $mime = 'application/x-hdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpg': $mime = 'image/jpg'; break;
case 'png': $mime = 'image/png'; break;
case 'cdf': $mime = 'application/x-netcdf'; break;
case 'hdf': $mime = 'application/x-hdf'; break;
default: $mime = 'application/force-download';
}
ob_clean_all(); // clean output buffer before the file transfer set-up
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
readfile($file_name); // push it out
$fh = fopen($myFile,'a') or die('cannot open file: ' . $myFile); // open download log file
$stringData = date("Y/m/d H:i:s")." ".$user['user_name']." ".$_SERVER['REMOTE_ADDR']." downloaded ".$file_name."\n";
fwrite($fh, $stringData); // write download transaction
fclose($fh);
exit(); // at sometime, we need to clean-up the tmp directory
}
Can anyone help?
Bookmarks