file_get_contents 和 fread 有什么区别
有什么区别
$contents = file_get_contents("folder/somefile.txt")
和
$handle = fopen("folder/somefile.txt", "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
在性能、文件指针处理和内存管理方面
?如果操作系统允许,file_get_contents 是否使用 mmap
?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
fread 对可以读取的字符数量有限制,并且它更适合解析数据。
file_get_contents 对输入没有限制(据我所知)。这用于外部 API 访问等。
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()
读取二进制数据,file_get_contents()
以字符串形式返回数据。fread()
reads binary data,file_get_contents()
returns the data as a string.奇怪的结果! file_get_contents() 函数应该是 fopen 的包装器,但 fopen 和 fread 的解耦似乎会使性能变慢。
http://www.ebrueggeman.com/blog/php_benchmarking_fopen
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