skip to Main Content

I am making a script to bulk import csv data into a mysql DB table. I am looping through the data and checking if the value already exist in database. If not then insert it into the table.
But the script is giving 504 gateway timeout error. I tried increasing the max_execution_time but nothing is working, the script timeout occurs in around 7-9 minutes.
I tried increasing the timeout in the cPanel and also using the below code

ini_set('max_execution_time', 1800);
ini_set('default_socket_timeout', 1800);

But nothing is working, prior thanks to any help.
PHP version is 7.3

2

Answers


  1. All the webservers usually have a separate timeout as well than php like Apache and IIS around 6-7 mins by default. You need to check which software your host is running and check its documentation. Although usually you can’t change these setting only the server owners (I’m guessing you are renting a server/space). But first it’s worth a try to echo ini_get('max_execution_time'); after you set them to see if it changed anything or has been overridden by the server (on shared host there might be a ‘hard cap’ which you can’t increase further.

    Login or Signup to reply.
  2. You should try MYSQL TRANSACTION to insert csv records. Start TRANSACTION before insert operation start and COMMIT TRANSACTION after all process end. Here is small example which will help you.

    $file_name = 'https://yoursite.com/upload_csv_folder/' . $csv_file_name;
    
    $fileSize = file($file_name);
    $totalRow = count($fileSize);
    
    $handle = fopen($file_name, "r");
    $header = fgetcsv($handle, 1000, ",");
    
    mysql_query("START TRANSACTION");
    while (($csv_data = fgetcsv($handle, 1000, ",")) !== false) {
        // Do your duplicate checking operation
        // Insert record
    }
    mysql_query("COMMIT");
    

    Thankyou.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search