PHP Classes

Download without saving the file first

Recommend this page to a friend!

      Create ZIP File  >  All threads  >  Download without saving the file first  >  (Un) Subscribe thread alerts  
Subject:Download without saving the file first
Summary:Created function to download the archive without saving to disk
Messages:8
Author:PM75
Date:2008-07-11 06:04:25
Update:2010-11-23 06:48:51
 

  1. Download without saving the file first   Reply   Report abuse  
Picture of PM75 PM75 - 2008-07-11 06:04:26
I created a function for this class, to force download of the archive without having to save it to disk first. Add the following function to the class:

public function dowloadZippedFile($archiveName) {
$headerInfo = '';

if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}

// Security checks
if( $archiveName == "" ) {
echo "<html><title>Photo Directory - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
exit;
}
elseif ( ! file_exists( $archiveName ) ) {
echo "<html><title>Photo Directory - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
exit;
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($archiveName));
print $this->getZippedfile();
}

  2. Re: Download without saving the file first   Reply   Report abuse  
Picture of PM75 PM75 - 2008-07-11 06:23:29 - In reply to message 1 from PM75
Oops, found a bug in my function. You have to remove the check for existance of the file on disk...

  3. Re: Download without saving the file first   Reply   Report abuse  
Picture of Er. Rochak Chauhan Er. Rochak Chauhan - 2008-07-11 08:02:28 - In reply to message 2 from PM75
yeh chech out the exampleScript.php last line..

@unlink($fileName);

:)

  4. Re: Download without saving the file first   Reply   Report abuse  
Picture of THITH THITH - 2010-11-14 20:47:55 - In reply to message 2 from PM75
hi,
unreadable archive with your function...
can you help me?
thanks

  5. Re: Download without saving the file first   Reply   Report abuse  
Picture of Er. Rochak Chauhan Er. Rochak Chauhan - 2010-11-15 06:05:32 - In reply to message 4 from THITH
Hi

I guess you need to open that unreadable archive in text editor. It would be having the error message.

Lemme know if that works.

Rochak Chauhan.

  6. Re: Download without saving the file first   Reply   Report abuse  
Picture of THITH THITH - 2010-11-23 00:01:20 - In reply to message 5 from Er. Rochak Chauhan
hi
archive ok in serveur ($createZipFile->getZippedfile())
archive unreadle when its downloaded(forceDownload($zipName);)
<code>

$rand=md5(microtime().rand(0,999999));
$zipName=$uploadDirectory.$rand."_".$zipName;
$fd=fopen($zipName, "wb");
$out=fwrite($fd,$createZipFile->getZippedfile());
fclose($fd);
//$createZipFile->dowloadZippedFile($zipName);
$createZipFile->forceDownload($zipName);
//print_r($createZipFile);
@unlink($zipName);

</code>

  7. Re: Download without saving the file first   Reply   Report abuse  
Picture of THITH THITH - 2010-11-23 00:14:15 - In reply to message 6 from THITH
new post
works on localhost but not in server.
version php are same for both of them (5.1.4)
many many thanks.

  8. Re: Download without saving the file first   Reply   Report abuse  
Picture of Er. Rochak Chauhan Er. Rochak Chauhan - 2010-11-23 06:48:51 - In reply to message 7 from THITH
Well in that case the issue is not with the code. Try debugging:

1) Make sure you have enough permissions to the class/file on the live server.

2) You have enough memory allocated to load all those files in the memory.

3) Check the error log or enable "display_errors" to find the exact issue.


Best Regards,
Rochak Chauhan