You are here: Re: file_get_contents vs readfile « PHP Programming Language « IT news, forums, messages
Re: file_get_contents vs readfile

Posted by Good Man on 09/10/07 15:04

howa <howachen@gmail.com> wrote in news:1189433262.872289.319540@
19g2000hsx.googlegroups.com:

> Target: To fetch a file using PHP and send to user
>
> Method 1:
>
> echo file_get_contents( $file_path );
>
> Method 2:
>
> readfile( $file_path );
>
>
> Which one is better?
>

I send files to users via PHP all the time, as my documents are stored
above the "www" directory. I use a modification of "readfile", found on
the manual page by chrisputnam at gmail dot com... had to use this
function because "readfile" has a knack for ending the file streaming
early on large files:


function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;

}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация