Lugi's avatar
Level 21

Cannot upload file locally

I'm trying to setup the file upload procedure locally on my Windows PC.

This is the code in my view:

<form method="POST" action="{{ route('storemyfile') }}">
    {{ csrf_field() }}
    <input type="text" name="name">
    <input type="file" name="myfile" />
    <button type="submit">Save</button>
</form>

And this is the code in my Controller:

public function storeMyFile(Request $request)
{
    if ($request->hasFile('myfile'))
    {
        return "HAS FILE.";
    }
    return "NO FILE.";
}

When I get to the page and choose the file to upload I keep getting "NO FILE." as an answer.

What did I miss ?

Thanks

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

You missed the encoding type for your form: multipart/form-data is required to upload a file through the form.

<form method="POST" action="{{ route('storemyfile') }}"  enctype="multipart/form-data">
MehdiElMellali's avatar
<form method="POST" action="{{ route('storemyfile') }}"  enctype="multipart/form-data">
  {{ csrf_field() }}
    <input type="text" name="name">
    <input type="file" name="myfile" />
    <button type="submit">Save</button>
</form>

enctype="multipart/form-data" IS RESPONSIBLE FOR UPLOAD FILE

Lugi's avatar
Level 21

@tykus many thanks. I was already trying to add enctype="multipart/form-data" but to instead of element. It seems I have to start to learn basics from scratch...

Please or to participate in this conversation.