any suggestions to improve perormance?
Feb 12, 2021
5
Level 5
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);
}
Please or to participate in this conversation.