当前位置:网站首页>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
边栏推荐
- RAID磁盘阵列与RAID5的创建
- Postman batch production body information (realize batch modification of data)
- 计算饼状图百分比
- File upload and download of robot framework
- [pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
- Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- 关于局域网如何组建介绍
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- Installation and management procedures
猜你喜欢

Mock test

磁盘管理与文件系统

vim编辑器的实时操作

How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
![[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return](/img/f5/15b3731e75eb4d861bd9d29ae244da.png)
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return

Change the icon size of PLSQL toolbar

∑GL-透视投影矩阵的推导

Day 10 abnormal mechanism

How magical is the unsafe class used by all major frameworks?

深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
随机推荐
Creation of RAID disk array and RAID5
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
Calculate pie chart percentage
Talk about browser cache control
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
MySql主从复制
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
About JMeter startup flash back
Detailed explanation of Niuke - Gloves
How to build tiktok user trust and drive fan growth
NVIDIA graphics card driver error
04 Lua operator
关于局域网如何组建介绍
正则过滤内网地址和网段
Pytorch: the pit between train mode and eval mode
扫码登录的原理你真的了解吗?
Report FCRA test question set and answers (11 wrong questions)
JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
Summary according to classification in sail software
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed