file_get_contents 和 fread 有什么区别

发布于 2024-12-01 12:11:17 字数 325 浏览 1 评论 0原文

有什么区别

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舂唻埖巳落 2024-12-08 12:11:18

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.

无远思近则忧 2024-12-08 12:11:18

fread() 读取二进制数据,file_get_contents() 以字符串形式返回数据。

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

凶凌 2024-12-08 12:11:18

奇怪的结果! 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文