0

I am trying to loop through the array from a csv file to insert new information. However, once the program hits the loop, it throws a time exception. I'm totally lost on this one... Any help would be greatly appreciated. Thank you in advance.

function readAndWriteToCsv(String $fullDirectory, Array $images, String $pageTitle)
{
  //echo '<br>';

  $newDirectoryPath = $fullDirectory . '\\CSVproducts.csv';
  $lines2 = file($newDirectoryPath);
  echo 'Directory string made: ' . $newDirectoryPath;
  echo '<br>';

  //$lines = file('D:\\Dropbox (Personal)\\white-performance\\scrap\\1-PAIR-OF-BBC-HEAD-GASKET-GASKETS-MULTI-LAYERED-STEEL-4.585\\pageTitle.txt');


  $csvTests = array_map('str_getcsv', $lines2);

  var_dump($csvTests);
  echo '<br>';
  echo '<h2>'.$csvTests[1][3].'</h2>';
  echo '<br>';
  echo '<h2>'. sizeof($csvTests).'</h2>';
  echo '<br>';
  echo '<h2>'.$csvTests[1][28].'</h2>';
  echo '<br>';
  echo '<h2>'.$csvTests[2][3].'</h2>';
  echo '<br>';
  echo '<h2>'.$csvTests[2][28].'</h2>';
  echo '<br>';
  echo '<h2>'.$csvTests[3][3].'</h2>';
  echo '<br>';
  echo '<h2>'.$csvTests[3][28].'</h2>';
  echo '<br>';

  $arraySize = sizeof($csvTests);
  echo $arraySize;

  for($i = 0; i < $arraySize; $i++)
  {
    $productName = $csvTests[i][3];

    if($productName == $pageTitle)
    {
      //array_fill_keys('images', 'TESTING YOUR MOMA');
      $csvTests[i][3][28] = implode(", ", $images);
      //array_push($csvTests[1][28], $image);
      echo '<br>';
      echo 'Images put in key value: ';
      echo $csvTests[i][28];
      echo '<br>';
    }

  }

}
Joshua Trimm
  • 63
  • 2
  • 13

2 Answers2

2

Your script takes too long to run! You've only got 30 seconds max to finish what you're doing. No exception is thrown here, this is PHPs timer running out of time to run the program. Without this, broken PHP scripts would run forever and cripple your server

If you're feeding in a truly massive CSV, you're going to need to split it up into smaller files, or use a CLI command which will give you unlimited time.

Additionally, you want to do as little as possible inside the loop. It's also very possible that with a huge file, that you might run out of memory space to store all this data.

So:

  • use a cli command instead of a browser request
  • split your CSV up into smaller files
  • do less on each loop iteration

Remember, browser requests should be fast! If you're taking 10 seconds then you're doing something you're not supposed to, and if 30 seconds isn't enough then there's something seriously wrong! Probably processing a giant CSV file on a slow server, though even on a super fast high end dedicated hosting, you shouldn't do this, especially not on the frontend

Tom J Nowell
  • 9,588
  • 17
  • 63
  • 91
0

Adding to Tom J Nowells answer, you can increase also just increase the PHP execution time limit if using a CLI and/or splitting the CSV into smaller files aren't viable options.

LuisD
  • 50
  • 5