laravel 5.2 中的输入文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38119094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:04:56  来源:igfitidea点击:

Input file in laravel 5.2?

phplaravelfile-uploadlaravel-5.2

提问by Caius

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.

我正在尝试上传文件,但是当请求到达控制器时它失败了。失败我的意思是如果我尝试$request->hasFile("filename")总是返回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...

有什么线索吗?我什至检查了 php.ini 以查看是否有大小限制,但它全部设置为 32M 作为 MAMP 的专业默认设置...

回答by Imtiaz Pabel

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

检查你可能已经忘记了添加enctype="multipart/form-data"form

回答by Yudi Yohanes Septian Gotama

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() !!}
    
  1. 通过使用 HTML

    <form action="{{url('dev/tester')}}" method="post" enctype="multipart/form-data">
    
  2. 通过使用 laravel 表单和 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!

这应该像一个魅力!

回答by linx

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

尝试将 添加enctype="multipart/from-data"到您的表单中,然后它应该可以工作!