PHP 5 readfile Problem

April 26, 2006

PHP LogoIn 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:
  1. <?php
  2. function readfile_chunked($filename,$retbytes=true)
  3. {
  4.    $chunksize = 1*(1024*1024); // how many bytes per chunk
  5.    $buffer = '';
  6.    $cnt =0;
  7.    // $handle = fopen($filename, 'rb');
  8.    $handle = fopen($filename, 'rb');
  9.    if ($handle === false)
  10.    {
  11.        return false;
  12.    }
  13.    while (!feof($handle))
  14.    {
  15.        $buffer = fread($handle, $chunksize);
  16.        echo $buffer;
  17.        if ($retbytes)
  18.        {
  19.            $cnt += strlen($buffer);
  20.        }
  21.    }
  22.    $status = fclose($handle);
  23.    if ($retbytes && $status)
  24.    {
  25.        return $cnt; // return num. bytes delivered like readfile() does.
  26.    }
  27.    return $status;
  28. }
  29. ?>

code, development, file, file handling, flash, large file size, php, programming, readfile

Tags: , , , , , , , ,

Related:


Comments

3 Responses to “PHP 5 readfile Problem”

  1. http://idisk.mac.com/fmp3musicdownloads/Public/free-legal-music-downloads.html on December 10th, 2007 9:42 am

    Hello people07058af13da10b0b8597ae4afcb12cbf

  2. Great boys on January 31st, 2008 12:18 pm

    Great boys2762b0cbb15a0cbb1bd2843ee287eb55

  3. speed on February 4th, 2008 10:27 am

    jhgk

Got something to say?





User contributed tags: t (366) - "php readfile()" (144) - readfile php (69) - f (46) - buffering porno (44) - php readfile problem (43) - buffering porn (42) - php5 fopen (38) - php5 require( (30) - spiderman3 crack (29) - porno buffering (28) - +php +readfile +"large files" (26) - php readfile swf (26) - php readfile image (25) - php5 readfile (25) - readfile swf (23) - buffering porno video (21) - php readfile not working (19) - php readfile bug (18) - readfile php5 (17) - fread php5 (15) - buffer porno (15) - readfile large files (14) - buffering porn video (14) - readfile vs fread (14) - php 5 readfile() (14) - php readfile size (13) - spiderman 3 crack not working (13) - php readfile_chunked (12) - readfile (11) - spiderman 3 crack (11) - php readfile remote (11) - php5-swf (11) - php readfile "large file" (10) - readfile php "large file" (10) - Spider-Man.3 crack (10) - php filesize not working (10) - readfile php large files (9) - videos porno buffer (9) - php5 read-file (9) - porn buffering (9) - fopen php5 (9) - readfile in php (9) - readfile image (9) - buffering video porn (8) - php readfile problems (8) - fopen not working (8) - fread vs. readfile (8) - flash readfile (8) - no buffering porn (8) - buffering video porno (8) - spiderman 3 crack problem (8) - spiderman3 nodvd (8) - readfile bug (7) - php5 fopen problem (7) - crack spiderman3 (7) - php5 fread (7) - php readfile rss (6) - readfile_chunked php (6) - READFILE not working (6) - readfile "php 5" (6) - php readfile filesize (6) - file handling in php5 (6) - php readfile big file (6) - php5 readfile problem (6) - readfile problem (6) - swf porn (6) - fopen swf (6) - php readfile big files (6) - readfile_chunked "large files" (5) - php readfile image problem (5) - readfile swf php (5) - porno buffer (5) - ReadFile problems (5) - php readfile "file size" (5) - video porno buffering (5) - spiderman 3 no-dvd crack (5) - php readfile video (5) - porn with no buffering (5) - readfile rss (5) - readfile(" buffer php (5) - no buffering porn videos (5) - php read file (5) - readfile.php (5) - php @readfile images (5) - buffer free porn (5) - php 5 not working (4) - php readfile chunk (4) - fopen readfile (4) - free porn no buffering (4) - php readfile() buffer (4) - filesize php5 (4) - readfile problem php (4) - spiderman 3 working crack (4) - php readfile() flash (4) - spiderman 3 crack no-DVD (4) - php image readfile (4) - php5 readfile bug (4) - readfile +php +buffer (4) - php 5 fopen (4) - sheep problems (4) - php readfile large (4) - porno video buffering (4) - 300 torrent not working (4) - php idisk (4) - readfile vs fopen (4) - flash php readfile() (4) - php 5 fopen files size (4) - buffering porno videos (4) - php fopen not working (4) - bug readfile (4) - videos porno em buffer (4) - problème readfile php (4) - porn no buffering? (4) - php fopen swf (4) - php return swf (3) - videos pornos de buffering (3) - Crack NODVD SpiderMan 3 (3) - php readfile big size (3) - php+readfile+swf (3) - Spider-Man3 nodvd (3) - php5 require() not working (3) - free buffering porn (3) - php fopen "large file" (3) - no buffering porns (3) - buffer video porno (3) - probleme readfile (3) - readfile in php5 (3) - swf readfile (3) - videos porno buffering (3) - videos pornos buffering (3) - php readfile swf files (3) - php swf readfile (3) - readfile remote (3) - readfile php problem (3) - buffering porn videos (3) - no buffering porn site (3) - buffering media files php (3) - readfile big files (3) - readfile php image (3) - no buffering porn video (3) - readfile php bug (3) - porn video buffering (3) - no buffering free porn videos (3) - readfile function in php (3) - readfile chunk (3) - readfile() php not working (3) - readfile flash php (3) - readfile images php (3) - porno:buffering (3) -