duanfen1992 2015-05-12 11:41
浏览 17
已采纳

使用太多内存读取文件PHP

So, I'm trying to parse a text file that has 24MB and 314134 lines. The problem is, I feel like my script is using way too much memory.

This is the code:

if(file_exists($filePath)) {
            $data = file_get_contents($filePath);

            $lines = explode("
", $data);
            foreach ($lines as $line) {
                //Split the line.
                $spllitedLine = explode(';', utf8_encode($line));

                //Get the fields by their index.
                $localidade = !empty($spllitedLine[3]) ? $spllitedLine[3] : '';
                $codigo_postal = $spllitedLine[14] . '-' . $spllitedLine[15];

                $morada = (!empty($spllitedLine[5]) ? $spllitedLine[5] : ' ') . ' ' .
                    (!empty($spllitedLine[6]) ? $spllitedLine[6] : ' ') . ' ' .
                    (!empty($spllitedLine[7]) ? $spllitedLine[7] : ' ') . ' ' .
                    (!empty($spllitedLine[8]) ? $spllitedLine[8] : ' ') . ' ' .
                    (!empty($spllitedLine[9]) ? $spllitedLine[9] : '');

                //Create a new CTT location and save it to the Database.
                $location = new CttLocations();
                $location->address = preg_replace('/\s\s+/', ' ', $morada);
                $location->location = $localidade;
                $location->zipcode = $codigo_postal;
                $location->save(false);

                //Unset the variables to free space.
                unset($location);
                unset($line);
                unset($morada);
            }
        }

This is currently using 153MB of memory and it's not even in the half of the file. I've read that using fopen() fgets() and fclose() it's a better solution but I was using roughly the same amount of memory with those methods. What am I doing wrong? I thought by unsetting the variables I would free some much needed space. I think 150MB is WAY too much for an operation like this. Any thouhgts?

  • 写回答

1条回答 默认 最新

  • dqdjfb2325 2015-05-12 11:48
    关注

    This :

     $data = file_get_contents($filePath);
    

    Is way to heavy for big files.

    This is how you read a file line by line :

    $handle = fopen("inputfile.txt", "r");
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            // process the line read.
        }
    
        fclose($handle);
    } else {
        // error opening the file.
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab