2014-09-23 27 views
0

我有一個2Mb大小的csv文件,並且有管道分隔符。我想取第一行並替換它的數據,然後重新保存文件。下面是我做的:在PHP中替換CSV行,內存錯誤

//Creating a new first row with the modified data. 
$file = fopen($path,"r");//$path is where the file is located : outputs/my_file.csv 
$size = filesize($path); 
$firstLine = fgetcsv(fopen($path,"r")); //$firstLine has all the data of the first row as array 
fclose($file); 
$firstLine = explode("|", $firstLine[0]);//To get each column row 
$newHeader = array(); 
    for($i = 0; $i<sizeof($firstLine); $i++){ 
     if($i == 4){ 
      array_push($newHeader, "modified column in row 1 ");//Only column 4 in row 1 is modified 
     }else{ 
      array_push($newHeader, $firstLine [$i]); 
     } 
    } 
$Header = implode("|", $newHeader); 

//Creating the new csv file 
$row = 0; 
    while (($data = fgetcsv(fopen($path,"r"), "|")) !== false) { 
     if($row == 0){ 
      $data[0] = $Header; 
     } 
     $newCsvData[] = $data; 
    } 
    return $newCsvData; //I wanted to display the new content of the csv before saving it 

此代碼應打印csv文件的新內容,我會保存,但我得到一個錯誤:用盡536870912個字節允許內存大小(試圖分配332個字節),如何能我以非常快的方式做到這一點?該文件大約是19122行。

感謝

回答

1

如果只有2MB,或許將整個文件讀入內存,然後寫了一個新的文件(覆蓋以前的文件)。下面是一些輔助功能,幫助用戶讀取和寫入文件,我敢肯定你在編輯結果數組精通:

/** 
    * Reads a file into an array 
    * 
    * @param $FILE string the file to open 
    * 
    * @return $lines The Lines of the file as an array 
    */ 
public static function readFile($FILE) { 

    $lines = array(); // the array to store each line of the file in 

    $handle = fopen($FILE, "r"); 
    if ($handle) { 

     // $FILE successfully opened for reading, 

     while (($line = fgets($handle)) !== false) { 
      $lines[] = $line; //add each line of the file to $lines 
     } 

    } else { 
     throw new Exception("error opening the file..."); 
    } 

    fclose($handle); // close the file 

    return $lines; // return the lines of the file as an array 
} 

/** 
    * Writes the $lines of a file into $FILE 
    * 
    * @param $FILE string The file to write 
    * @param $lines array An array containing the lines of the file 
    * 
    * @return $result int|NULL The number of bytes written, or null on failure. See: php.net/fwrite#refsect1-function.fwrite-returnvalues 
    */ 
public static writeFile($FILE, $lines) { 

    // Add newline at the end of each line of the array 
    // output is now a single string which we will write in one pass 
    // (instead of line-by-line) 
    $output = implode("\n", $lines); 

    $handle = fopen($FILE, "w+"); 
    if ($handle) { 

     // $FILE successfully opened for writing, write to the file 
     $result = fwrite($handle, $output); 

    } else { 
     throw new Exception("error opening the file..."); 
    } 

    fclose($handle); // close the file 

    return $result; // The number of bytes written to the file, or NULL on failure 
} 
+0

是的我使用類似於你的解決方案修復它,謝謝。 – 2014-09-23 19:29:50

0
<?php 
$source = fopen('filename','r'); 
$destination = fopen('newfilename','w'); 
//write new header to new file 
fwrite($destination,"your|replacement|header\n"); 
//set the pointer in the old file to the second row 
fgets($source); 
//copy the entire stream 
stream_copy_to_stream($source,$destination); 
//rename the newfilename to the old filename 
rename('newfilename','filename'); 
//check what memory we used: 
var_dump(memory_get_peak_usage()); 

這導致在高峯期使用的內存142260個字節爲2MB文件。順便說一句:2GB文件的內存使用情況正好如果我在這裏測試它。