简体   繁体   中英

PHP .csv file overwriting

I've been writing a registration system with PHP which works via command-line. And I've faced a problem with overwriting text. I want to save all data not replace the old one. How can this problem be solved?

function addUser($username, $lastname, $mobile, $mobile2, $comment) {
    $myArray = [$username, $lastname, $mobile, $mobile2, $comment];
    $myFileString = implode(':', $myArray);
    $myDataArray = [];
    array_push($myDataArray, $myFileString);
    $myFile = fopen("users.csv", "w+");
    foreach ($myDataArray as $line) {
    fputcsv($myFile,explode(',',$line));
    }
    fclose($myFile);
    echo $myFile;
}

Open the file for appending instead of for writing :

$myFile = fopen("users.csv", "a+");

From the manual :

Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM