替换PHP中的CSV行,内存错误

时间:2014-09-23 17:34:01

标签: php csv

我有一个大小为2Mb的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行。

由于

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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文件。 BTW:2GB文件的内存使用情况正是我在这里测试的。