Thursday, March 2, 2017

Laravel 5.2 uploaded file always null

I'm trying to upload a file but it always returns null in my controller. Here's the error I'm getting:

FatalThrowableError in FileController.php line 25:
Fatal error: Call to a member function getClientOriginalExtension() on null

Form

<form enctype="multipart/form-data" method="post" action="">
    <label for="file_upload">Upload file</label>
    <input id="file_upload" type="file" name="file">
    <input type="submit" value="Upload">
</form>

Route (not calling any middleware)

Route::post('file/upload', [
    'as'   => 'upload',
    'uses' => 'FileController@handleUpload',
]);

Controller

public function handleUpload(Request $request)
{
    $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'adp';
    if (!file_exists($dir)) mkdir($dir);

    $extensions = AppPlatform::lists('package_extension')->toArray();

    $extension = $request->file('file')->getClientOriginalExtension(); // this line throws the nullreference exception
    ...
}

I may have overlooked something very obvious but can't seem to find where the problem lies at the moment.

EDIT

When I look at the request in chrome's developer tools I can see the file is sent in the post request correctly.

------WebKitFormBoundaryTLB02G6sLMEE2fMM
Content-Disposition: form-data; name="file"; filename="app-release.apk"
Content-Type: application/octet-stream


------WebKitFormBoundaryTLB02G6sLMEE2fMM--




via Fester

Advertisement