I'm using Respect Validation to attempt to validate a file upload. The problem I'm having is the validation always fails, even though the correct image has been uploaded.
Here's my code:
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());
}
}
The error I'm getting is:
{
"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 } }"
}
As suggested in the comments I tried using $validator->assert($files['image']);
. This didn't work and yields the following error:
{
"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\" })`"
}
The $files
variable contains:
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)
}
}
And $_FILES
contains:
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)
}
}
As suggested in the comment I've tried $validator->assert($_FILES['image']['tmp_name']);
and get the following error:
{
"status": false,
"data": null,
"message": "All of the required rules must pass for \"/private/var/folders/ry/kgy3g6v96s71jg3htn6r5rt80000gn/T/php8E5apg\""
}
I've tried to Google to see how others are doing it but was unable to find anything useful.
Try to do this:
var_dump(v::image()->validate($_FILES['image']['tmp_name']));