您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP如何实现读取一个1G的文件大小

2024/2/29 7:33:22发布17次查看
【背景】:由于 file函数是一次性将所有内容读入内存,而php为了防止一些写的比较糟糕的程序占用太多的内存而导致系统内存不足,使服务器出现宕机,所以要想点好办法。
  【思路】:
01 思路1:利用php执行linux的命令,将一个文件内容(a.log)复制到另一个文件中(b.log) cat a.log >>b.log02 思路2:用php执行linux的head命令,获取内容,一行行写入另一个文件(b.log)中。 cat a.log|wc -l sed -n '1 p' a.logfileputcontents('b.log’, $content, file_append);
【代码实现】:
1 //02.读取1g的文件内容 2 public function testgetlarge() 3 {/*{{{*/ 4 #01. 方法1 直接cat后重定向 5 #if('usecat' == 'usecat') 6 if(0 && 'usecat' == 'usecat') 7 { 8 `cat a.log>>b.log`; 9 } 10 11 #02. 使用flie_put_contents ,一行一行读取并追加写入 12 if('usefileputcontents' == 'usefileputcontents') 13 {/*{{{*/ 14 $linenum = `cat a.log|wc -l`; 15 for($i=1; $i<= $linenum; $i++) 16 { 17 $content = `sed -n '$i p' a.log`; 18 //文件追加 19 file_put_contents('b.log', $content, file_append); 20 } 21 22 }/*}}}*/
以上就是php如何实现读取一个1g的文件大小的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product