php的file_get_content不会销毁读取到内存中的文件内容吗?该如何解决

WBOY
Release: 2016-06-13 13:28:10
Original
1741 people have browsed it

php的file_get_content不会销毁读取到内存中的文件内容吗?
前阵子给自己写的小程序:遍历指定文件夹内的文件,并搜索指定内容

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->// 这种用法很占用内存,几乎每次都出错
$content = file_get_contents($file);

// 成功
$content = '';
$fp = fopen($file, 'r');
$content .= fread($fp, 10240);
// 即使没有fclose(),在函数内调用仍然会顺利运行完毕
Copy after login

我的猜测是file_get_contents()不会在调用结束后销毁读取的文件内容内存。但是为什么呢?我不认为php的开发者比我蠢,但这看起来确实是个愚蠢的设计

------解决方案--------------------
file_get_contents()

等价于
fopren()
fread(filesize())
fclose()

即一次性将文件读入内存,如遇到巨大的文件就会出现内存不足
而你 $content .= fread($fp, 10240);
每次才读 10k 当然就不容易发生问题了
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!