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...
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.
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') ).
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.
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.
Check if you may have forgotten to add enctype="multipart/form-data"
in form
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With