New PHP may not stream video files and content lenght for all files streamed is zero

Repost from a diffeent forum from this morning:

The following problem could certainly be some other than with the new PHP version.

All I know is that up until yesterday morning, where I did the entire upgrade as described in the upgrade thread, I was able to use PHP code to stream video files to surfers, using code similar to the below example, and now it streams absolutely nothing, actually it seems from from the receivers end that the content-lenght is zero.

function sendfile($sFilename){
$nFileSize = filesize($sFilename);
$sContentType = PAYC_GetContentType($sFilename);
header (“Content-type: $sContentType”);
header (“Content-length: $nFileSize”);
if (isset($_GET[‘saveas’]) && ($_GET[‘saveas’]==1))
header (“Content-Disposition: attachment; filename=”. $sFilename);
else
header (“Content-Disposition: inline; filename=”. $sFilename);
readfile($sFilename);
}

Somehow it is going wrong before it tries to output the file, but if I put in some test lines to echo the file lenght it echo’s the file size correctly. All streaming worked perfectly unitl yesterday morning, and I have triple checked that the files to be delivered are on my server in the right location, so I suspect that this problem has to do with the upgrades.

Streaming pictures works fine, HOWEVER suddenly after the upgrade the content-lenght is now zero!?!

Follow up:

I have now heard from the support team at the microbilling company I am using, PayAsYouClick.com after having given them my ftp information, so they could check my php code, and here is what they are saying:

"Basically what’s happening is that php is overwriting our content-length header with the actual size of the content being sent. That’s why we were seeing content-length=0 even though it could happily read the file.

The second thing that’s happning is that its refusing to output that file. This may be because the file is too big for readfile, although it worked before of course. This causes the body not to be written, it outputs content-length=0, and stops.

I’m trying another method of outputing the file."

Question, how can I downgrade to my old faithful PHP version?

Don’t use readfile. It may be using up all the memory allowed by the PHP.INI file (which may have been overwritten on upgrade).

I suggest doing a while loop reading a limited number of bytes at a time, like 4 MB.


function sendfile($sFilename)
{
  $file = fopen($sFilename);
  $nFileSize = filesize($sFilename);
  $sContentType = PAYC_GetContentType($sFilename);
  header ("Content-type: $sContentType");
  header ("Content-length: $nFileSize");
  if (isset($_GET['saveas']) && ($_GET['saveas']==1))
   header ("Content-Disposition: attachment; filename=". $sFilename);
  else
   header ("Content-Disposition: inline; filename=". $sFilename);
  
  // Set read size, if less than 4M set read size to file size
  if($fileSize > 4096) $readSize = "4096";
  else $readSize = $fileSize;
  
  while( $filedata_temp = fread($file, $readSize) )
    echo $filedata_temp;

}