当前位置:网站首页>PHP高效读大文件处理数据
PHP高效读大文件处理数据
2022-04-23 16:44:00 【代元培】
<?php
// 直接用file()会报致命错误 内存耗尽;如 $file = file($file);
// 报错内容 取决于memory_limit配置
// Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 840860368 bytes) in /home/work/*.php on line *
// php bigFileRead.php test.txt
if(!isset($argv[1])){
echo 'The first parameter is null. =>>> [php bigFileRead.php 1.txt]'.PHP_EOL;die;
}else{
$file = $argv[1];
if(!file_exists($file)){
echo 'file does not exist.'.PHP_EOL;die;
}
}
function effReadFile($path){
if($handle = fopen($path, 'r')){
// 函数检查是否已到达文件末尾
while(!feof($handle)){
yield trim(fgets($handle));
}
fclose($handle);
}
}
// 读大文件 使用
$glob = effReadFile($file);
while ($glob->valid()) {
// 当前行文本
$line = $glob->current();
// 逐行处理数据
echo $line.PHP_EOL;
// 指向下一个,不能少
$glob->next();
}
版权声明
本文为[代元培]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sosemseo/article/details/124299137
边栏推荐
- 文件系统读写性能测试实战
- About background image gradient()!
- vim编辑器的实时操作
- 计算饼状图百分比
- How does flash cache data in memory?
- Bytevcharts visual chart library, I have everything you want
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
猜你喜欢
Sail soft calls the method of dynamic parameter transfer and sets parameters in the title
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
OMNeT学习之新建工程
Cartoon: what are IAAs, PAAS, SaaS?
How magical is the unsafe class used by all major frameworks?
Force buckle - 198 raid homes and plunder houses
Idea of batch manufacturing test data, with source code
File upload and download of robot framework
Zhongang Mining: Fluorite Flotation Process
随机推荐
DanceNN:字节自研千亿级规模文件元数据存储系统概述
Use case labeling mechanism of robot framework
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
杂文 谈谈古典的《拆掉思维里的墙》
STM32__03—初识定时器
05 Lua 控制结构
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
MySQL master-slave synchronization pit avoidance version tutorial
loggie 源码分析 source file 模块主干分析
Zhongang Mining: Fluorite Flotation Process
Detailed explanation of UWA pipeline function | visual configuration automatic test
File upload and download of robot framework
LVM and disk quota
On the security of key passing and digital signature
文件操作详解(2)
NVIDIA graphics card driver error
Derivation of Σ GL perspective projection matrix
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
Do you really understand the principle of code scanning login?
On the value, breaking and harvest of NFT project