如何使用尊重验证库验证 $_FILES
How to validate $_FILES using Respect Validation library
我正在使用 Respect Validation 来尝试验证文件上传。我遇到的问题是验证总是失败,即使已上传正确的图片。
这是我的代码:
public function updateProfilePicture(Request $request, Response $response, $args = []) {
$files = $request->getUploadedFiles();
if(empty($files['image'])) {
throw new AppException('Invalid image');
}
// Validate image
$validator = v::key('image', v::image());
try {
$validator->assert($files);
// $validator->assert($_FILES);
} catch(NestedValidationException $e) {
throw new AppException($e->getMessage());
}
}
我得到的错误是:
{
"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\""
}
我已经尝试 Google 看看其他人是怎么做的,但找不到任何有用的东西。
尝试这样做:
var_dump(v::image()->validate($_FILES['image']['tmp_name']));
我正在使用 Respect Validation 来尝试验证文件上传。我遇到的问题是验证总是失败,即使已上传正确的图片。
这是我的代码:
public function updateProfilePicture(Request $request, Response $response, $args = []) {
$files = $request->getUploadedFiles();
if(empty($files['image'])) {
throw new AppException('Invalid image');
}
// Validate image
$validator = v::key('image', v::image());
try {
$validator->assert($files);
// $validator->assert($_FILES);
} catch(NestedValidationException $e) {
throw new AppException($e->getMessage());
}
}
我得到的错误是:
{
"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\""
}
我已经尝试 Google 看看其他人是怎么做的,但找不到任何有用的东西。
尝试这样做:
var_dump(v::image()->validate($_FILES['image']['tmp_name']));