2017-04-11 20:11
浏览 81


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"]."";
        $file1 = file_get_contents($file, null);

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

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



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)

图片转代码服务由CSDN问答提供 功能建议

我正在迭代一些csv文件(没有超过320MB的文件)来清理和格式化 使用以下脚本记录我如何需要它们:

  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); 

我收到了 跟随错误无论我在php.ini中设置内存限制有多高,或者在我可以释放内存的情况下取消设置varbeles。我试过设置 限制在脚本内部,但仍然没有去。 我的机器不缺RAM,足以在我需要时操作文件时轻松存储。

错误:(!)致命错误:内存不足(已分配) 683147264)(试图分配364322204字节)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
    解决 无用
    打赏 举报

相关推荐 更多相似问题