PHP 5 readfile Problem
April 26, 2006
In PHP 5.0.x there is a known bug that can cause large (2MB+) files to be handled incorrectly by the readfile function. We first discovered this while working with Tiki Wiki. Today we ran into it again with the way we stream secured SWF Flash files off the file system.
Luckily, I found this great function on PHP.net in the user comment section. Simply drop this function definition in place and use it instead of readfile and the problem is gone!
-
<?php
-
function readfile_chunked($filename,$retbytes=true)
-
{
-
$chunksize = 1*(1024*1024); // how many bytes per chunk
-
$buffer = '';
-
$cnt =0;
-
// $handle = fopen($filename, 'rb');
-
if ($handle === false)
-
{
-
return false;
-
}
-
{
-
echo $buffer;
-
if ($retbytes)
-
{
-
}
-
}
-
if ($retbytes && $status)
-
{
-
return $cnt; // return num. bytes delivered like readfile() does.
-
}
-
return $status;
-
}
-
?>
Tags: code, development, file, file handling, flash, large file size, php, programming, readfile
Comments
3 Responses to “PHP 5 readfile Problem”
Got something to say?

Hello people07058af13da10b0b8597ae4afcb12cbf
Great boys2762b0cbb15a0cbb1bd2843ee287eb55
jhgk