Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

getting timeout error when importing data from csv.

I have a csv file with 1k data in each file but it is giving timeout problem after 73 records only. any tweaks to speed up? i can't use queue as i am using shared hosting.

my code is,

public function postUploadData(Request $request)
    {
        $request->validate([
            'uploadfile' => 'required|required|mimes:csv,txt',

        ]);

        $file = $request->file('uploadfile');
        $filename = $file->getClientOriginalName();
        // File upload location
        $location = 'uploads';

        $file->move(public_path() . '/uploadfiles/', $filename);
        

        $header = NULL;
        $data = array();
        $delimiter = ',';

        if (($handle = fopen(public_path() . '/uploadfiles/' . $filename, 'r')) !== FALSE) {
            while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
                if(!$header) {
                    $header = $row;
                } else {
                    $data[] = array_combine($header, $row);
                }
            }


            fclose($handle);
        }

        $uuid = md5(rand(1, 10) . microtime());

        $createdata = [
                'file_name' => $filename,
                'type'  => 'uploadtype',
                'uuid' => $uuid,
                'upload_date' => date('Y-m-d'),
                'uploaded_by' => Auth::user()->id,
                ];

       UploadHistory::create($createdata);


        for($i=0;$i<sizeof($data); $i++) {

            $jsonPayload = json_encode([[
                'field1' => $data[$i]['field1'],
                'field2' => $data[$i]['field2'],
                'field3' =>$data[$i]['field3'],
                'field4' => $data[$i]['field4'],
                'field5' => $data[$i]['field5'],
                'field6' => $data[$i]['field6'],
                'field7' => $data[$i]['field7'],
                'field8' => $data[$i]['field8'],
                'field9' => $data[$i]['field9'],
                'field10' => $data[$i]['field10'],
                'field11' => $data[$i]['field11'],
                'field12' => $data[$i]['field12'],
                'field13' => $data[$i]['field13'],
                'field14' => $data[$i]['field14'],
 				'field15' => $data[$i]['field15'],
				'field16' => $data[$i]['field16'],
            ]]);

            $client = new GuzzleClient();

            $headers = [
                'Authorization' => '789789',
                'Content-Type' => 'application/json',
            ];

            $response = $client->post('https://apiurl.com/pushdata', [
                'body' => $jsonPayload,
                'headers' => $headers,
              ]);
              
              $responsedata = json_decode($response->getBody(), true); // returns an array
          

              

              // return $data['statusMessage'];

              if($responsedata)
              {
                
                $color = $responsedata['status'] == "Success" ? 'green' : 'red';
               

                $uploaddata = [
                    'status' => $responsedata['status'],
                    'message' => serialize($responsedata['statusMessage']),
                    'color'  => $color,
                    'uuid' => $uuid,
                    'uploaded_by' => Auth::user()->id,
                ];

              } else {

                $uploaddata = [
                        'status' => 'Fail',
                        'message' => 'No Response From API',
                        'color'  => 'red',
                        'uuid' => $uuid,
                        'uploaded_by' => Auth::user()->id,
                    ];
              }

              UploadDataTrack::create($uploaddata);

              
            
         }

         $msg = 'Data Uploaded successfully. <a href="'. url('track-activity/uploaddata/') . '/'.$uuid.'" target="_blank"> click here  </a>  to see the log';

         return redirect('admin-dashboard')->with('message',$msg);
    }

0 likes
5 replies
Deekshith's avatar

@tray2 i am using shared server as there is no option to configure the server.

Tray2's avatar

If you can't access the php.ini or use queue workers you pretty much are screwed. It's the max execution time in the php.ini that determines how long a script can run and 30 seconds is default.

I still recomend that you use queue workers for processing csv files.

To get around this you have two options. Host it yourself or host it in a cloud.

sr57's avatar

Best solution is a vps as @tray2 wrote.

Anyway you have 2 other solutions

-1- On shot import, use ftp, ... to manually upload data to your server

-2- Web UI : create a 'auto-refresh' page (script) that import only part of your file (at least only one line)

Please or to participate in this conversation.