[SOLVED] stream length problem
Posted: Sun Aug 08, 2004 3:49 pm
Hi,
I am trying to write a PHP script to do some work before a user can download a file. The URL on my test web page points to my .php script. It will pass a numeric ID to the script I will translate it to a file name on the server but for testing I have hard coded the file name. The problem is that the streaming of the file stops after 251834 bytes.
I am using PHP 4.3.8 and BadBlue Web Server 2.5 on Windows 98.
The server we will eventually use runs Apache version 1.3.31 (Unix), PHP 4.3.8 on Linux but I am just trying out different ways to do this at the moment.
Here is my script:
This is the usual "force download" script with a few alterations. I am reading the file in 128k chunks because I am worried about the amount of memory fpassthru would use for large files.
PHP.EXE appears to hang using 0 processor time according to SysInternals Process Explorer. When I kill the PHP.EXE process the Save Dialog appears on the client browser and it saves the first 251834 bytes of the file. Using simple fpassthru($fdl); causes the same problem. If I do not echo the file the file read is OK and takes a fraction of a second.
I am quite new to PHP programming, I would be very grateful if someone can help. I tried flushing the stream after every 128k read, but that didn't work. Am I doing something really stupid?
Thanks in advance.
I am trying to write a PHP script to do some work before a user can download a file. The URL on my test web page points to my .php script. It will pass a numeric ID to the script I will translate it to a file name on the server but for testing I have hard coded the file name. The problem is that the streaming of the file stops after 251834 bytes.
I am using PHP 4.3.8 and BadBlue Web Server 2.5 on Windows 98.
The server we will eventually use runs Apache version 1.3.31 (Unix), PHP 4.3.8 on Linux but I am just trying out different ways to do this at the moment.
Here is my script:
Code: Select all
<?php
// Download a file using fpassthru()
$fileDir = "images"; // supply a path name.
$fileName = "test.mp3"; // supply a file name.
$fileString=$fileDir.'/'.$fileName; // combine the path and file
// translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
$fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);
}
// make sure the file exists before sending headers
if(!$fdl=@fopen($fileString,'rb'))
{
die("Cannot Open File!"); // display file id, not file name
}
else
{
$flen=filesize($fileString);
header("Content-Type: binary\n");
header("Accept-Ranges: bytes\n");
header("Content-Disposition: attachment; filename=".basename($fileString)."\n");
header("Content-length: " . $flen . "\n");
rewind($fdl);
$buffsize=128000;
$nb=floor($flen/$buffsize);
$rem=$flen%$buffsize;
for($n=0;$n<$nb;$n++)
{
$contents = fread($fdl, $buffsize);
echo $contents;
}
if($rem!=0)
{
$contents = fread($fdl, $rem);
echo $contents;
}
fclose($fdl);
//fpassthru($fdl);
}
?>PHP.EXE appears to hang using 0 processor time according to SysInternals Process Explorer. When I kill the PHP.EXE process the Save Dialog appears on the client browser and it saves the first 251834 bytes of the file. Using simple fpassthru($fdl); causes the same problem. If I do not echo the file the file read is OK and takes a fraction of a second.
I am quite new to PHP programming, I would be very grateful if someone can help. I tried flushing the stream after every 128k read, but that didn't work. Am I doing something really stupid?
Thanks in advance.