This is what I'm using to return a pdf file from a data directory to a user
when they request it.
As mentioned in a previous post be sure to validate the file requested.
$tfile = 'real_path_data_path_to_file';
$tp = pathinfo($tfile);
header("Content-Type: application/pdf");
header("Content-disposition: attachment; filename=".$tp['basename'].";");
header("Content-Length: ".filesize($tfile));
header('Content-Transfer-Encoding: Binary');
header('Accept-Ranges: bytes');
header('ETag: "'.md5($tfile).'"');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
readfile_chunked($tfile);
// Exit;
// or just make sure nothing else is passed or it could cause the file to be
invalid.
The readfile_chunked function was used because the pdf's I'm using are
rather large at times, around 20 to 30 meg. With file_get_contents the
browser plugin would hang if the user tried to viewing the pdf in the
browser. And it caused something when trying to download the file, but don't
remember what exactlly atm.
I found this online somewhere, php.net forums I think.
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 8*1024; // ~8k chunks
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile()
does.
}
return $status;
}
Hope that helps.
Terre
-----Original Message-----
From: rollockg [mailto:[EMAIL PROTECTED]
Sent: Friday, August 15, 2008 5:57 PM
To: [email protected]
Subject: [fw-general] Downloading a file from server
I've been able to successfully upload a pdf file to my server, now I want to
reverse the process. I just have no idea how to go about it using Zend
Framework?
Can I please have some assistance?
Gina-Marie Rollock
--
View this message in context:
http://www.nabble.com/Downloading-a-file-from-server-tp19005166p19005166.htm
l
Sent from the Zend Framework mailing list archive at Nabble.com.