Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

php - how to successfully fix uploaded csv file error in laravel?

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. enter image description here

question from:https://stackoverflow.com/questions/65939614/how-to-successfully-fix-uploaded-csv-file-error-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...