dongyi7669 2017-04-11 20:11
浏览 86
已采纳

运行我的php脚本时为什么一直出现内存分配错误?

I'm iterating through a handful of csv files (none that exceed more than 320MB) to clean up and format the files how I need them using the follow script:

while($row = mysqli_fetch_array($rsSelect)){        

    $fileName = $row["file_name"];

    if(strpos($fileName,'DATA') == true){

        $file = $dir.$row["manifest_files"]."";
        echobr($file);
        $file1 = file_get_contents($file, null);

        unset($file);
        $file2 = str_replace('","','    ', $file1);

        unset($file1);
        $file3 = str_replace('"','', $file2);

        file_put_contents($dir.$row["file_name"].".txt",$file3,FILE_USE_INCLUDE_PATH);
        unset($file2);
        unset($file3);

}

I receive the following error no matter how high I set my memory limits within php.ini or unsetting variabeles where I can to free up memory. I've tried setting the limit inside the script as well and still no go. My machine has no shortage of RAM, enough to easily store when manipulating the files as I need them.

ERROR: ( ! ) Fatal error: Out of memory (allocated 683147264) (tried to allocate 364322204 bytes)

  • 写回答

1条回答 默认 最新

  • doukeng7426 2017-04-11 20:19
    关注

    Using file_get_contents() requires the entire file to be read into memory in one big chunk. You're probably better off using fopen() with a loop around fgets(). This will read just one line at a time:

    $fp = fopen('file.csv', 'r');
    while (($line = fgets($fp)) !== false) {
        // $line is the whole line
    }
    

    Alternatively, you might be able to use fgetcsv() and process individual fields as needed:

    $fp = fopen('file.csv', 'r');
    while (($row = fgetcsv($fp)) !== false) {
        // $row is an array of values from the line
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么