6

I'm trying to upload an image via an HTML form in Laravel 5.5. I have included the enctype="multipart/form-data" attribute, still nothing happens.

Form code:

<form method="POST" enctype="multipart/form-data">
  {{ csrf_field() }}
  <div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
      <input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
    </div>
  </div>

  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Route (web.php) code:

Route::post('smartphones/entry', 'HomeController@s_submit')->name('s_submit');

Controller code:

public function s_submit() {
  if (Input::hasFile('m_photo')) {
      // doing something
  }

  else {
      echo 'Nothing happened';
  }
}

'Nothing happened' is echoed out when I submit the form.

It's interesting that when I do this:

public function s_submit(Request $request) {
    $input = Input::all();
    dd($input);
}

I see:

array:1 [
"m_photo" => UploadedFile {#210 ▶}
]

It's like the image is getting passed, but I'm unable to retrieve it. Please help.

7 Answers 7

4

This can happen when PHP max_file_size is not set to a size that allows the file you are trying to upload to be sent. This causes hasFile returns false, when, for example, file->getClientOriginalName() works.

Try to check upload_max_filesize or post_max_size in your php.ini, or try with a smaller file to check if it works.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Laerte, this was the problem. The upload_max_filesize was set to 2M. What do you recommend I should set it to? 10M?
It depends on your needs. When you set a big value, you must check your resources and to guarantee that all the uploads during the life cycle of your app will succeed.
3
if (Input::hasFile('m_photo')) {
    $destinationPath = '/uploads/app/';
    $file = $request->file('m_photo');
    $filename = $file->getClientOriginalName();
    $file->move(public_path() . $destinationPath, $filename);
    $filename_to_save_in_db = $destinationPath . $filename;
}

1 Comment

if (Input::hasFile('m_photo')) is returning false in my case :(
1

Get the file with:

$file = $request->m_photo;

Or with:

$file = $request->file('m_photo');

https://laravel.com/docs/5.5/requests#retrieving-uploaded-files

1 Comment

Already tried that, public function s_submit(Request $request) { if(request()->hasFile('m_photo')) { // doing something } else { echo 'Nothing happened'; } } 'Nothing happened' gets echoed out :(
0

You have forgot to put action in Your html Form :

put action="/smartphones/entry" or action="{{route('s_submit')}}"

<form method="POST" enctype="multipart/form-data" action="{{route('s_submit')}}">
  {{ csrf_field() }}
  <div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
      <input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
    </div>
  </div>

  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Create images folder in public folder

in your controller

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


public function s_submit(Request $request) {
  if($request->hasFile('m_photo')){
            $filenameWithExt=$request->file('m_photo')->getClientOriginalName();
            $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
            $extension=$request->file('m_photo')->getClientOriginalExtension();
            $fileNameToStore=$filename.'_'.time().'.'.$extension;
            request()->m_photo->move(public_path('images'), $fileNameToStore);
        }
        else{
           $fileNameToStore='noimage.jpg';      
        } 
}

5 Comments

When action is not specified, the form is submitted to the same URL. Hence I did not put action. I tried your solution as well though, still same error.
if(request()->hasFile('m_photo')) returns false.
While reading this I thought it'll work, maybe the underscore is causing the issue. But no, it's still not working, even when I changed the name to 'file' :(
have u changed your function to s_submit(Request $request) . ?
Hey Saurabh thank you so much for helping me out. The problem was php's max upload limit in php.ini file. Once I changed that, everything started working as expected :)
0

Try this:

public function s_submit()
{
            request()->validate([
                'm_photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ]);

            $imageName = time().'.'.request()->m_photo->getClientOriginalExtension();
            request()->m_photo->move(public_path('images'), $imageName);
            return back()
                ->with('success','You have successfully upload image.')
                ->with('m_photo',$imageName);
}

Blade:

{!! Form::open(array('route' => 's_submit','files'=>true)) !!}
<div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
{!! Form::file('m_photo', array('class' => 'form-control-file space')) !!}
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
{!! Form::close() !!}

1 Comment

This code is from expertphp website. I tried this, doesn't work :(
0

For future visitors, the accepted answer is the right answer. I wanted to share one more thing. I also faced similar issue and I had set the upload_max_filesize and post_max_size variables to 20M, which was quite enough. But I still faced the issue. So, I increased to 500M, then it worked. It was really strange because I was uploading a file of less than 1 MB size.

Comments

0

In Laravel, when you're handling file uploads, you should type-hint the Request object in your controller method instead of using Input. Also, you should ensure that your form has the correct action attribute pointing to the route where you handle the form submission. Here's how you can modify your code

use Illuminate\Http\Request;

public function s_submit(Request $request) {
  if ($request->hasFile('m_photo')) {
      // Retrieve the uploaded file
      $photo = $request->file('m_photo');

      // Do something with the file, such as saving it
      // For example:
      $photo->store('photos');

      return 'File uploaded successfully';
  } else {
      return 'No file uploaded';
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.