Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input file in laravel 5.2?

I'm trying to upload a file, but it fails when the request lands to the controller. With fails i mean that if i try $request->hasFile("filename") always returns false.

Is there some specific field that I have to specify in the view?

This is a snippet of the view:

<body>
    <form action="{{url('dev/tester')}}" method="POST">
        {{csrf_field()}}
        <input type="file" name="file">
        <button type="submit">Test</button>
    </form>
</body>

And here is the controller

class Tester extends Controller
{
    public function index(Request $request)
    {
        if($request->hasFile('file'))
        {
            dd('Got the file');
        }

        dd('No file');
    }

    public function testView()
    {
        return view('tests.file_upload');
    }
}

I always get returned 'No file'.

Any clue? I've even check the php.ini to see if there was a size limitation but it's all set to 32M as MAMP's pro default settings...

like image 558
Caius Avatar asked Jun 30 '16 09:06

Caius


People also ask

How do I input a file in laravel?

In a view file, we need to generate a file input by adding the following line of code. Form::file('file_name'); In Form::open(), we need to add 'files'=>'true' as shown below. This facilitates the form to be uploaded in multiple parts.

What is input function in laravel?

input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).

What is $request in laravel?

Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

What is GetClientOriginalName in laravel?

The function GetClientOriginalName() is used to retrieve the file's original name at the time of upload in laravel and that'll only be possible if the data is sent as array and not as a string. Hence, you must add enctype="multipart/form-data" whenever you are creating a form with input fields for files or images.


1 Answers

Check if you may have forgotten to add enctype="multipart/form-data" in form

like image 108
Imtiaz Pabel Avatar answered Sep 17 '22 22:09

Imtiaz Pabel