Son's avatar
Level 1

Request->input() doesn't work with file upload Javascript

Hi everyone,

I've got an error with Request in Laravel 5.3, it seems that $request->input doesn't work with file upload, meanwhile the old ways $request['file'] and dynamic $request->file still work very well. Do you have any idea why?

$file = $request->input('file'); // return null

HTML/JS

{{ csrf_field() }}

<input type="file" id="fileinput">


<script>
    var UPLOADURL = 'route/uploadfile';

    $("#fileinput").on("change", function(event) {
        var file = event.target.files[0];
          
        var fd = new FormData();
        fd.append("file", file);
       
        $.ajax({
            type: 'POST',
            url:  UPLOADURL,
            headers: { 'X-CSRF-Token': $('input[name="_token"]').val() },
            cache: false,
            processData: false,
            contentType: false,
            data: fd,
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                // console.log(error['responseText']);
                $(document.body).html(error['responseText']);
            }
        });
    });
</script>

Controller: route/uploadfile

    $fileName = 'test.jpg';
    $file = $request->input('file'); // return null
        //$file = $request['file']; //$file = $request->file; // OK
    $file->move(config('app.paths.upload_dir'), $fileName);
    return 'https://'.$_SERVER['HTTP_HOST'].config('app.paths.upload_url').$fileName;
0 likes
9 replies
popcone's avatar

Documentation here

For files you have to use $request->file() (in your case $request->file('file'))

OR

Dynamically $request->file

1 like
J_shelfwood's avatar

Check jeffrey's file 101 tutorial. He names a few things you should keep in mind on both your browser and backend when trying to upload files.

Such as @habeebnet mentioned.

1 like
Son's avatar
Level 1

Thanks guys,

@habeebnet : I tried to use the following code but no file uploaded in public/images

$request->file->storeAs('images', 'test.jpg');

Actually, $request->file('file') work very well in my case. Here I upload file using Javascript (Ajax) to assign file content to 'file' argument not from an HTML form with file input.

var file = event.target.files[0];
var fd = new FormData();
fd.append("file", file);
....
$.ajax({
...
    data: fd,
...

});
popcone's avatar

@Son can u try after changing the name of file filed to "myFile" or something else instead of "file".

Son's avatar
Level 1

Here it is, @habeebnet :

  • $request['myFile']; OK
  • $request->input('myFile'); >> NULL, meanwhile $request->input('testStrArg') >> OK
  • $request->myFile->storeAs('images', 'test.jpg'); no file stored at public/images/test.jpg
  • $request->file('myFile')->storeAs('images', 'test.jpg'); no file stored at public/images/test.jpg

Javascript

var file = event.target.files[0];
var fd = new FormData();
fd.append("myFile", file);
....
$.ajax({
...
    data: fd,
...

});
edoc's avatar

@Son I think your file was saved in storage/app/images

2 likes
popcone's avatar

@Son as edoc said it should be saved in your "strage/app/images"

If you want to save the files to "public/images", you have to create a new disk in "config/filesystems.php"

'myuploads' => [
            'driver' => 'local',
            'root' => public_path(),
            'visibility' => 'public',
        ],

Then upload using

$request->file('myFile')->storeAs('images', 'test.jpg', 'myuploads');
1 like
Son's avatar
Level 1

Oh yeah, there it is in default location storage/app/images. Thanks men

Still, I am a bit curious about the fact that $request->input('myFile') returns NULL while $request['myFile'] returns the very uploaded file.

popcone's avatar

@Son why can't you go through Illuminate\Http\Request and check all the methods.

I think $request will all the request data. but $request->input() is only for GET and POST (Not sure though). Please do some experiments yourself.

1 like

Please or to participate in this conversation.