473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 3551
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
0
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
4
by: Fred Flintstone | last post by:
This one baffles me. I'm using VS.Net 2005 and write desktop apps that need built in help. So logically, I figure maybe VS has a help system component built in so I search the help. Hey! ...
3
by: lord.zoltar | last post by:
I've managed to get a nice little chm help system written. Now I need to display it! I added a HelpProvider to my MDIParent form and set the namespace of the HelpProvider to be the help file. So...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.