I am trying to solve the problem of processing a large csv file (12194 lines) with goods, I read the file using the parseCSV library

There is a "model code" column in the file, I need to go through each line and add a random prefix to the model code, for example: MODELCODE_PREF987 To do this, I write this code using the library methods:

  <?php set_time_limit(0); require_once('parsecsv.lib.php'); $fileCSV = 'file_pred.csv'; $csv = new parseCSV(); $csv->delimiter = "\t"; $csv->parse($fileCSV); $count = count($csv->data); for($i = 0; $i<$count; $i++){ $ID = $csv->data[$i]['Код модели'] . "_" . rand(0,1000); $csv->data[$i]['Код модели'] = $ID; $csv->save(); } 

But the script hangs exactly on the data modification, if you just make a conclusion, then everything quickly works out. And most likely it should be so, I am only starting to understand php , and therefore my code is most likely not correct. Please give an example (addition to the comment) method of recording the file

  protected function _wfile($file, $string = '', $mode = 'wb', $lock = 2) { if ($fp = fopen($file, $mode)) { flock($fp, $lock); $re = fwrite($fp, $string); $re2 = fclose($fp); if ($re != false && $re2 != false) { return true; } } return false; } 

    1 answer 1

    Try $csv->save() from the for() loop:

     //... for($i = 0; $i<$count; $i++){ $ID = $csv->data[$i]['Код модели'] . "_" . rand(0,1000); $csv->data[$i]['Код модели'] = $ID; } $csv->save(); 

    You get that after each change there is file re-saving, and it’s enough to save the file once, at the end, after all changes are taken into account. I didn’t work with this library, but I looked at the library code on the githaba and I think that should help.

    • Thank you! It all worked. - Sergey Zaigraev
    • I noticed the following, for example, if the file weighs 6 mb then after rewriting it always becomes smaller and weighs 4 mb. Even if the file is only 1 MB, then after rewriting it becomes smaller. What could be trouble? from zi used library? - Sergey Zaigraev
    • data is not lost? I repeat that I didn’t work with this library - Igor Karpenko
    • they are lost, the number of lines becomes less, the record is produced by this method: (I will add a write method to the answer) can there be a reason in fwrite? tried to pass the third parameter of this function filezesize ($ file) but there is nothing to change - Sergey Zaigraev
    • On localhost, everything works fine, on the battle server there is no, need it means that you need to change some settings? - Sergey Zaigraev