在 Laravel 5.8 中验证 .tar 文件上传

我有这个代码


$inputs    = Input::all();

$file      = Input::file('file');

$validator = Validator::make($request->all(), [

    'file' => 'required|max:50000|mimes:application/x-tar',

]);


if ($validator->fails()) {


    $messages = $validator->messages();

    dd($file,$messages); <<<< --- keep executing 



    return Redirect::to('/vnf-packages/create')->withErrors($validator)->withInput()

    ->with('error','Something is wrong with your upload image');

} else {

..

}

我不知道为什么它一直进入失败块。我确实用.tar.


我的代码中是否缺少任何内容?


更新

如您所见,我确实上传了 .tar 文件


dd($file,$messages);

UploadedFile {#446 ▼

  -test: false

  -originalName: "config-vnfp-vyos-567.tar"

  -mimeType: "application/x-tar"

  -error: 0

  #hashName: null

  path: "/private/var/tmp"

  filename: "phpNaZGxr"

  basename: "phpNaZGxr"

  pathname: "/private/var/tmp/phpNaZGxr"

  extension: ""

  realPath: "/private/var/tmp/phpNaZGxr"

  aTime: 2020-03-09 15:10:28

  mTime: 2020-03-09 15:10:28

  cTime: 2020-03-09 15:10:28

  inode: 14703131

  size: 140

  perms: 0100600

  owner: 70

  group: 0

  type: "file"

  writable: true

  readable: true

  executable: false

  file: true

  dir: false

  link: false

}

MessageBag {#454 ▼

  #messages: array:1 [▼

    "file" => array:1 [▼

      0 => "The file must be a file of type: tar."

    ]

  ]

  #format: ":message"

}

更新 2

'file' => 'required|max:50000|mimes:tar', //fail 

'file' => 'required|max:50000|mimes:application/x-tar', //fail 

'file' => 'required', //success 


温温酱
浏览 121回答 1
1回答

POPMUISE

我尝试使用.tar文件测试上传,当我使用该getMimeType方法时,它返回以下内容:application/gzip所以你的验证规则应该是:'file' => 'required|mimetypes:application/gzip',如果你想使用这个mimes规则,你会想要这样的:'file' => 'required|mimes:gz',通过一些研究,我发现了以下内容:https ://superuser.com/questions/901962/what-is-the-correct-mime-type-for-a-tar-gz-file您可以在 中找到有关验证器如何验证这两个规则的更多信息Illuminate/Validation/Concerns/ValidatesAttributes.php。该validateMimes方法使用文件的guessExtension()方法,该方法返回gz.$value->getPath() !== '' && in_array($value->guessExtension(), $parameters)并且validateMimeTypes方法检查使用getMimeType()返回的文件方法application/gzip。return $value->getPath() !== '' &&&nbsp; &nbsp; (in_array($value->getMimeType(), $parameters) ||&nbsp; &nbsp; in_array(explode('/', $value->getMimeType())[0].'/*', $parameters));
打开App,查看更多内容
随时随地看视频慕课网APP