浅析php中如何在有限的内存中读取大文件

yipeiwu_com6年前PHP代码库
正常情况下,我们可以使用fseek来读取,好处就是不会一次性读取,以下代码只适合边取边处理的情况,不适合一次性读取一次性处理。
可以用以下办法生成测试文件
复制代码 代码如下:

$file_handle = fopen("./csdn.txt", "rb+");
for ($index1 = 1; $index1 <= 2000000; $index1++) {
    fwrite($file_handle, 'http://jb51.net'.$index1."\r");
}
fclose($file_handle);

读取处理代码如下:
复制代码 代码如下:

$i = 0;
$now = '';
while ($i >= 0) {
    if ($i>10) {
        break;
    }
    fseek($file_handle, 0, SEEK_CUR);
    $now = fgetc($file_handle);//可以自己写个判断false表示文件到头
    if ($now == "\r") {
        echo '找到断点';
    }
    echo $now;
    $i++;
}
fclose($file_handle);

相关文章

php自动适应范围的分页代码

复制代码 代码如下:<?php function page($page,$total,$phpfile,$pagesize=10,$pagelen=7){  &...

php错误日志简单配置方法

本文实例讲述了php配置错误日志的方法。分享给大家供大家参考,具体如下: php.ini: ; 错误日志 log_errors = On ; 显示错误 display_errors...

PHP开发中的错误收集,不定期更新。

Fatal error: Non-static method Conn::__construct() cannot be called statically in /file.php 没...

php的4种常见运行方式

SAPI:Server Application Programming Interface服务端应用编程端口。他就是php与其他应用交互的接口,php脚本要执行有很多中方式,通过web服...

PHP动态创建Web站点的方法

PHP有4个用于使用外部函数的函数:include()、include_once()、require()和require_once(). 为了使用它们,PHP脚本中将包括如下代码行: i...