Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : $request->hasFile() is not working

I have a form where I get the title, description, and an image. When I dd($requests->all());, It returns the following which is correct.

array:4 [
  "projectTitle" => "asd"
  "project_description" => "asd"
  "project_image" => "15940723_1336567063030425_9215184436331587115_n.jpg"
  "_token" => "eUj27iioySvIgut5Afu0ZHMgeVrO99a9e1o7Tw0w"
]

And I am storing the values as :

$project = new Portfolio;
$project->freelancer_id = Auth::user()->id;
$project->title = $request->get('projectTitle');
$project->description = $request->get('project_description');

if($request->hasFile('project_image')){
   $project_image = $request->file('project_image');
   $filename = time() . '.' . $project_image->getClientOriginalExtension();
   Image::make($project_image)->resize(197, 137)->save( public_path('/uploads/portfolios/' . $filename ) );
   $project->img = $filename;
}

$project->save();

But the img DB table field gets null.

The if($request->hasFile('project_image')) is not getting the field,

Also, I have a form where the method is POST and have enctype="multipart/form-data" and a for file I have <input type="file" name="project_image" id="project_image">.

What have I done wrong?


2 Answers

If everything's gonna correct in your code, then you might be missing the enctype, I had also missed that at startup,

<form action="{{ route('store') }}" method="POST" enctype="multipart/form-data">

Or if you use Form Helper, that enctype easily included with file=true field.

{!! Form::open(['route' => ['store'], 'file' => true]) !!}
like image 124
Maniruzzaman Akash Avatar answered Sep 05 '25 04:09

Maniruzzaman Akash


You need to check php directive upload_max_filesize in php.ini.

Generally its size is 2M defined as default. If you upload a file greater than this size, the Laravel function

$request->hasFile()

will return false.

like image 30
Mahesh Yadav Avatar answered Sep 05 '25 03:09

Mahesh Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!