【问题标题】:laravel check file extension in upload formlaravel 检查上传表单中的文件扩展名
【发布时间】:2019-12-30 12:56:34
【问题描述】:

这是html表单:

<form method = "post" action="...">
   <input type="text" name="subject" />
   <input type="file" type="files['new']['file']"/>
   <input type="text" type="files['new']['name']"/>
</form>

现在我尝试在服务器(laravel)中读取。

   public function chechkForm(request $request){
      $input = $request->all();
      dd(input['files']['new']['file' );
   }

结果是:

UploadedFile {#1356 ▼
  -test: false
  -originalName: "GK-Footer-Logo.png"
  -mimeType: "image/png"
  -error: 0
  #hashName: null
  path: "C:\Users\ali\AppData\Local\Temp"
  filename: "phpC3BF.tmp"
  basename: "phpC3BF.tmp"

现在我尝试使用这些来打印 mimetype:

$input['new0']['content']['mimeType']

$input['new0']['content']->mimeType

$input['new0']['content']->mimeType()

我怎么了?我得到了他们所有的“错误”!

【问题讨论】:

标签: php laravel forms laravel-5 php-7


【解决方案1】:

如果你想知道扩展名和 mime 类型

您可以使用 getClientOriginalExtension 方法和 getClientMimeType

或者其他你想用的可以在这里找到other methods

简单的用法和更多的laravel方式

你可以这样设置的表单

<form method = "post" action="..." enctype="multipart/form-data">
... other input ...
<input type="file" name="picture"/>
.... other input ....
</form>

在控制器中,您可以使用帮助文件('field_name')进行处理;

public function checkForm(Request $request){
  $file = $request->file('picture');
  dd( $file->getClientOriginalExtension() );
  dd( $file->getClientMimeType() );

}

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多