vilom10252's avatar

File not found at path: file.xlsx

I'm using Maatwebsite package for inserting some data into the Database by uploading an Excel file.

So here is my routes:

Route::get('import-form', 'Olympiad\OlympiadExamExecutionController@importForm');
Route::post('import-form', 'Olympiad\OlympiadExamExecutionController@executeImport')->name('addcutsom.execution');

And here is my Controller:

use App\Imports\CustomExamImport;
use App\Olympiad\OlympiadExamExecution;
use Excel;

public function importForm()
{
    return view('import');
}

public function executeImport(Request $request)
{
	Excel::import(new CustomExamImport, $request->file);
	return "Records are imported";
}

And here is the CustomExamImport:

class CustomExamImport implements ToModel
{
    public function model(array $row)
    {
		$user_id = DB::table('members')->where('mbr_national_code', $row[3])->value('mbr_usr_id');
        return new OlympiadExamExecution([
            'oex_correct_answer_count' => $row[7],
            'oex_wrong_answer_count' => $row[8],
            'oex_no_answer_count' => $row[9],
            'oex_score' => $row[10],
        ]);
    }
} 

And this is the Blade form:

<form action="{{ route('addcutsom.execution') }}" method="POST">
	@csrf
    <label class="control-label">Upload File:</label>
    <input class="form-control" name="file" type="file">

    <button class="btn btn-primary" type="submit">ثبت</button>  
</form>

Now when I try to upload an Excel file from my computer, I get this error:

File not found at path: file.xlsx

So what's going wrong out there?

0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

some validation would help ( make sure they actually submit a file) but your main problem is you need to add the enctype attribute to your form

<form action="{{ route('addcutsom.execution') }}" method="POST" enctype="multipart/form-data">

Please or to participate in this conversation.