如何使用Respect Validation库验证$ _FILES

时间:2017-01-23 10:00:14

标签: php validation respect-validation

我正在使用Respect Validation尝试验证文件上传。我遇到的问题是验证总是失败,即使上传了正确的图像。

这是我的代码:

{
  "status": false,
  "data": null,
  "message": "All of the required rules must pass for { \"image\": { \"name\": \"twitter.png\", \"type\": \"image/png\", \"tmp_name\": \"/private/var/folders/ry/kgy3g6v96s71jg3htn6r5rt80000gn/T/php7x3P6w\", \"error\": 0, \"size\": 210955 } }"
}

我得到的错误是:

$validator->assert($files['image']);

正如评论中所建议的那样,我尝试使用{ "status": false, "data": null, "message": "All of the required rules must pass for `[object] (Slim\\Http\\UploadedFile: { \"file\": \"/private/var/folders/ry/kgy3g6v96s71jg3htn6r5rt80000gn/T/phppJn1Uw\" })`" } 。这不起作用并产生以下错误:

$files

array(1) { ["image"]=> object(Slim\Http\UploadedFile)#120 (8) { ["file"]=> string(66) "/private/var/folders/ry/kgy3g6v96s71jg3htn6r5rt80000gn/T/php8sPpkD" ["name":protected]=> string(11) "twitter.png" ["type":protected]=> string(9) "image/png" ["size":protected]=> int(210955) ["error":protected]=> int(0) ["sapi":protected]=> bool(true) ["stream":protected]=> NULL ["moved":protected]=> bool(false) } } 变量包含:

$_FILES

array(1) { ["image"]=> array(5) { ["name"]=> string(11) "twitter.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(66) "/private/var/folders/ry/kgy3g6v96s71jg3htn6r5rt80000gn/T/phpm0Gv35" ["error"]=> int(0) ["size"]=> int(210955) } } 包含:

$validator->assert($_FILES['image']['tmp_name']);

正如评论中所建议的,我已尝试{ "status": false, "data": null, "message": "All of the required rules must pass for \"/private/var/folders/ry/kgy3g6v96s71jg3htn6r5rt80000gn/T/php8E5apg\"" } 并收到以下错误:

{{1}}

我曾试图让Google了解其他人是如何做到这一点但却找不到任何有用的东西。

1 个答案:

答案 0 :(得分:1)

尝试这样做:

var_dump(v::image()->validate($_FILES['image']['tmp_name‌​']‌​));