PHP 5 readfile Problem
April 26, 2006 | 3 Comments
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:
-
<?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
