Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between file_get_contents and fread

Tags:

file

php

file-io

What is the difference between

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

and

$handle = fopen("folder/somefile.txt", "r");
$contents = fread($handle, 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 ?

like image 358
Benjamin Crouzier Avatar asked Aug 25 '11 12:08

Benjamin Crouzier


1 Answers

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.

like image 50
Adam F Avatar answered Oct 02 '22 14:10

Adam F