468,256 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

Help! Streaming files via PHP get truncated...

Hi there folks

I've got an interesting little problem going on. On one of my projects,
I have users log in to retrieve files. The files themselves are stored
outside of the www directory on the server, so the only way they can be
downloaded is for PHP to stream them to the user.

My problem is that my downloads are always "completing" too soon, and
always around 1.6 -> 1.9 MB. So, when I try to download a 50MB file,
I'm told that the download is complete wayyy to early, and of course, the
file is nothing but garbage.

I have another script on the same server that will zip together a bunch
of files on the fly and stream it to the browser using a class that i did
not write personally. If I tell my system I want to download that 50MB
file as a .zip file, it works without a problem. If I tell the system I
want to download the 50MB file normally (its a PDF) using my
'stream.php' file, it truncates. ACK!

some variables that I got from phpInfo(); which may/may not have anything
to do with this problem (maybe this will help someone/me?):

output_buffering 4096 4096
max_execution_time 210 210
max_input_time 120 120

Anyways, here's the essence of the script I'm using - it's worked on
other servers (where i've been able to download large files
successfully).... can anyone offer any suggestions as to why the
downloads are truncating? or any other methods of doing what I want to
do?

thanks. try to ignore the word-wrap! oh... PHP Version 5.0.4
(www.entropy.ch Release 1) on a Mac PPC, running apache.

*******

<?php
session_cache_limiter("must-revalidate");
session_start();

/* downloading a file */

@$vKey = addslashes($_REQUEST['vKey']);

if ($vKey=="") { //no ID?
popup("We are having trouble locating this specific file.",$vBackPage);
exit;
}

databaseconnect(); //custom function connects to the database

//get the file
$row = singlequery("SELECT f.FilePath,f.FileName FROM Files AS f INNER
JOIN Folders ON f.FolderID = Folders.FolderID INNER JOIN Projects ON
Folders.ProjectID = Projects.ProjectID WHERE f.FileKey='$vKey'");
}

$vFileName = $row["FileName"];
$vFilePath = $row["FilePath"];

if($vFileName=="") {
popup("The file does not exist, or you do not have access to it.",
$vBackPage);
}

//now we stream the file, prompting a download
header("Cache-control: private");

// We'll be outputting a file
header('Content-Type: application/octet-stream');

// It will be called whatever the file name is called, and given the
// attachment Disposition to force the download

header('Content-Disposition: attachment; filename="'.$vFileName.'"');

// The source... see ya
readfile($vFilePath);

?>
many thanks,
GM
Jan 27 '06 #1
2 3239
Good Man <he***@letsgo.com> wrote in
news:Xn************************@216.196.97.131:

My problem is that my downloads are always "completing" too soon, and
always around 1.6 -> 1.9 MB.


hey, guess who JUST read the readfile(); page in the manual? ;)

Down in the comments section are the following posts... I've posted them
here in case anyone is trying to solve this problem in the future and
stumbled across this thread (hello future! do you have flying cars?)
COMMENTS:
( http://ca.php.net/manual/en/function.readfile.php )

flobee at gmail dot com
06-May-2005 02:17
regarding php5:
i found out that there is already a disscussion @php-dev about readfile
() and fpassthru() where only exactly 2 MB will be delivered. so you may
use this on php5 to get lager files

.... which eventually morphed via 'chrisputnam at gmai<snip>' into the
following function, which i have used successfully. thanks chris!
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$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;

}

Jan 27 '06 #2
d
"Good Man" <he***@letsgo.com> wrote in message
news:Xn************************@216.196.97.131...
Good Man <he***@letsgo.com> wrote in
news:Xn************************@216.196.97.131:

My problem is that my downloads are always "completing" too soon, and
always around 1.6 -> 1.9 MB.
hey, guess who JUST read the readfile(); page in the manual? ;)

Down in the comments section are the following posts... I've posted them
here in case anyone is trying to solve this problem in the future and
stumbled across this thread (hello future! do you have flying cars?)


Nicely done ;)

COMMENTS:
( http://ca.php.net/manual/en/function.readfile.php )

flobee at gmail dot com
06-May-2005 02:17
regarding php5:
i found out that there is already a disscussion @php-dev about readfile
() and fpassthru() where only exactly 2 MB will be delivered. so you may
use this on php5 to get lager files

... which eventually morphed via 'chrisputnam at gmai<snip>' into the
following function, which i have used successfully. thanks chris!
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$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;

}

Jan 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sarir Khamsi | last post: by
reply views Thread by tbatwork828 | last post: by
4 posts views Thread by dixie | last post: by
5 posts views Thread by Steve | last post: by
4 posts views Thread by Fred Flintstone | last post: by
3 posts views Thread by lord.zoltar | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.