开发者

What is the difference between file_get_contents and fread

开发者 https://www.devze.com 2023-03-31 05:02 出处:网络
What is the difference between $contents = file_get_contents(\"folder/somefile.txt\") and $handle = fopen(\"folder/somefile.txt\", \"r\");

What is the difference between

$contents = file_get_contents("folder/somefile.txt")

and

$handle = fopen("folder/somefile.txt", "r");
$contents = fread($h开发者_JS百科andle, filesize($filename));
fclose($handle);

in terms of performance, file pointer handling and memory managing ?

And is it true that file_get_contents uses mmap if OS allows it ?


fread has a limit on how many chars you can read, and its better for parsing data.

file_get_contents has no limit on the input (that I know of). This is used for external API access and such.


fread() reads binary data, file_get_contents() returns the data as a string.


Curious results! The file_get_contents() function is supposed to be a wrapper for fopen, but the decoupling of the fopen and fread seems to make performance slower. http://www.ebrueggeman.com/blog/php_benchmarking_fopen

0

精彩评论

暂无评论...
验证码 换一张
取 消