Home > OS >  PHP - read and update row in CSV file
PHP - read and update row in CSV file

Time:07-31

I want to open the CSV file, read rows one by one, and then update some rows with some Infos, function that I posted will read the CSV file row by row, but it will not update the row, it will add the new one, how to fix that?

function savetoCSV($name,$array){
        
    $fp = fopen($name, 'a'); //Open file for append
    //fputs($fp, $bom = (chr(0xEF) . chr(0xBB) . chr(0xBF) ));
    fputcsv($fp, $array); //
    print_r("Successfully saved information in the CSV file.\n");
    fclose($fp); //Close the file to free memory.
        
}

$file = fopen('de_login.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
    if ($line[1] == NULL){
         continue;
    }
    else{
        
   $client_name      = str_replace(array(" "),array(""),$line[0]);
   $account_id       = str_replace(" ","",$line[1]);
   $username         = str_replace(" ","",$line[2]);
   $pass             = str_replace(" ","",$line[3]);

    if (strpos($username, 'Login') !== false) {
            //skip
    }
    else{
        savetoCSV("de_login.csv",array($client_name, $account_id, $username, $pass, '', $pass ));
    }

    }
   
}
fclose($file);

CodePudding user response:

There is no way to do this without some kind of intermediary file/stream.

A file is not a set of lines. It's a stream of bytes. You can read it until you find bytes that represent a new line and then parse it as CSV, true. But at the end of the day, there are no lines in a file. Just bytes. Therefore, you never operate in terms of lines but always bytes.

Let's demonstrate how writing to an existing file works. We have a file called file.txt with the following content:

1,2,3,4,5,6,7,8,9,0,a,s,d,f,g,h,j,k,l

And we use fseek() to write a string example at position 10 in that file.

$fh = fopen('file.txt', 'r ');
fseek($fh, 10);
fwrite($fh, 'example');
fclose($fh);

The result:

1,2,3,4,5,example,0,a,s,d,f,g,h,j,k,l

As you can see, it just overwrites part of the file. It does not "move" the remaining bytes forward.

You may say: "but I opened the file in appending mode." Yes, you did. But appending means "appending to the end of the file." Not in the middle. There is just no such thing as appending in the middle of a file. If you open a file in that mode, fseek() won't even work, and you will always append at the end. To use the previous example:

1,2,3,4,5,6,7,8,9,0,a,s,d,f,g,h,j,k,l

$fh = fopen('file.txt', 'a');
fseek($fh, 10);
fwrite($fh, 'example');
fclose($fh);

The result is:

1,2,3,4,5,6,7,8,9,0,a,s,d,f,g,h,j,k,lexample

As you can see, there is only one way to solve it: with a separate file serving as an intermediary or a modified copy of your CSV.

CodePudding user response:

You have to loop through your array then instead of append, let it be write the line.

function savetoCSV($name,$array){
        
    $fp = fopen($name, 'w'); //Open file for write
     foreach ($array as $line) {
       fputcsv($file, $line);
     }
    print_r("Successfully saved information in the CSV file.\n");
    fclose($fp); //Close the file to free memory.
        
}
  • Related