eComEvo
eComEvo

Reputation: 12551

Laravel is not accepting files in request

I'm trying to do a simple HTML file upload.

<form accept-charset="utf-8" name="gallery" method="POST" action="/gallery">
    <input type="hidden" name="_token" value="fQ7arteCHmBFVfvQARWxxK3dXNgUJF40FdsaD3R">
    <label class="btn btn-default">
        Browse <input type="file" name="gallery[]" hidden multiple>
    </label>
</form>

I've traced it with Xdebug and I can see that $request->gallery shows an array of file names but the file count is still zero:

if (!$request->files->count()) {
    return redirect()->back()->with('error', 'Empty file list.');
}

The PHP global variable $_FILES is also empty.

This works fine when submitted over AJAX using something like Dropzone.js, but when I do it using a standard HTML5 form element it doesn't work.

Feel like I'm missing something obvious here.

Upvotes: 1

Views: 3623

Answers (3)

CodeToLife
CodeToLife

Reputation: 4141

you had to check the request on:

if($request->hasFile('file')){
//or isset($request->file){
... //you have access to 'request->file' only here
}

Upvotes: 0

Benni
Benni

Reputation: 130

Like the other said and do not forget the CSRF. (Default: on)

<form method="POST" action="/profile">
    {{ csrf_field() }}
    ...
</form>

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

<form accept-charset="utf-8" enctype="multipart/form-data" name="gallery" method="POST" action="/gallery">
<input type="hidden" name="_token" value="fQ7arteCHmBFVfvQARWxxK3dXNgUJF40FdsaD3R">
<label class="btn btn-default">
    Browse <input type="file" name="gallery[]" hidden multiple>
</label>

you are missing enctype=multipart/form-data in your form

Upvotes: 2

Related Questions