10

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

2
  • 6
    you are not sending enctype="multipart/from-data" from you from Commented Jun 30, 2016 at 9:32
  • what a shame! thanks @Imtiaz Pabel! These are the kind of mistakes that makes you loose a whole day! :D If you want to answer the question I will up vote it Commented Jun 30, 2016 at 9:35

3 Answers 3

14

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

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

Comments

11

You must enabling upload form to your form,

there is 2 ways to do it :

  1. By using HTML

    <form action="{{url('dev/tester')}}" method="post" enctype="multipart/form-data">
    
  2. By using laravel Form & HTML (https://laravelcollective.com/docs/5.2/html)

    {!! Form::open( [ 'action' => url( 'dev/tester' ), 'method' => 'post', 'files' => true ] ) !!}
        // Your form
    {!! Form::close() !!}
    

This should work like a charm!

Comments

2

Try adding the enctype="multipart/from-data" to your form, then it should work!

1 Comment

As I said in the previous comment these are that kind of forgiveness that helps you to lose a whole day (ironic) :D Thanks!

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.