Please I am trying to import a data in a csv file to my database. the system successfully uploads some of the data and throw unknown error during the upload process. Someone should kindly assist me.
*** Controller.php***
$this->validate($request, [
'file' => 'required',
]);
$upload = $request->file('file');
$filePath = $upload->getRealPath();
$file = fopen($filePath, 'r');
$header = fgetcsv($file);
$escapedHeader = [];
// data validation
foreach ($header as $key => $value) {
$escapedItem = strtolower($value);
array_push($escapedHeader, $escapedItem);
}
//looping through colums to get data
while ($columns = fgetcsv($file)) {
if ($columns[0] == "") {
continue;
}
//trim data
foreach ($columns as $key => &$value) {
$value = ucwords($value);
}
$data = array_combine($escapedHeader, $columns);
$name = $data['name'];
$contact = $data['contact'];
$email = $data['email'];
$role = $data['role'];
$interview_date = $data['interview_date'];
$appdata = new Applicant;
$appdata->name = $name;
$appdata->contact = $contact;
$appdata->email = $email;
$appdata->role = $role;
$appdata->interview_date = $interview_date;
$appdata->save();
}
return back()->with('status', 'Applicant Data Loaded Successfully');
Below is the error showing after importing some of the data from the csv file.
question from:
https://stackoverflow.com/questions/65939614/how-to-successfully-fix-uploaded-csv-file-error-in-laravel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…