27

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 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?

thechaoticpanda
  • 396
  • 2
  • 18
  • 1
    you might need to add -> enctype="multipart/form-data" .... to your form for it to see it as file instead of string. – Uche Dim May 10 '17 at 01:49

7 Answers7

42

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]) !!}
Maniruzzaman Akash
  • 4,610
  • 1
  • 37
  • 34
28

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Mahesh Yadav
  • 2,416
  • 20
  • 23
13

Be sure you have an 'files' => true option in your form definition like below:

{!! Form::open(['route' => ['uploadimage'], 'files' => true]) !!}

This option causes that your form will be render with enctype="multipart/form-data" option in HTML form tag, which is mandatory to upload a file using form

Or, if you are using html form, then make sure you have enctype="multipart/form-data" like:

<form action="{{ route('store') }}" method="POST" enctype="multipart/form-data">
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
7

I am using Laravel 5.5 and this worked for me (Found it on Laravel Docs https://laravel.com/docs/5.5/requests#files)

I replaced

if($request->hasFile('project_image')){...}

with

if ($request->file('project_image')!=null){...}
6

The error is not in your backend code. Looks like it's an error in your frontend code.

As you can see the output of dd($request->all()) returned

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

Your project_image is just a string whereas it should have been an UploadedFile object/instance like so:

array:4 [
  "projectTitle" => "asd"
  "project_description" => "asd"
  "project_file" => UploadedFile {#30 
    -test: false
    -originalName: "15940723_1336567063030425_9215184436331587115_n.jpg"
    -mimeType: "image/jpeg"
    -size: 5126
    -error: 0
  }
  "_token" => "eUj27iioySvIgut5Afu0ZHMgeVrO99a9e1o7Tw0w"
]
Paras
  • 9,258
  • 31
  • 55
0

Is the file getting stored on the server? Because you mention that the project_image field is null, but you are never saving anything to project_image, but you do try to save to the img field with $project->img = $filename;

z1haze
  • 431
  • 4
  • 7
0

I had this problem but for me, problem is that I had used Form::model instead of Form::open. It should be:

{!! Form::open(['url'=>'user/profile/' . $user->id, 'files'=>true) !!}
ParisaN
  • 1,816
  • 2
  • 23
  • 55