1

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.

9
  • Have you tried this? $validator->assert($files['image']); Commented Jan 23, 2017 at 10:07
  • @LucasdeOliveira tried that to no avail. I've updated my answer to show the error. Commented Jan 23, 2017 at 10:10
  • What does this variable $files return? If it is the same that $_FILES return, you can try to do this: $validator->assert($files['image']['tmp_name']); Commented Jan 23, 2017 at 10:17
  • @deoliveiralucas tried that too. I've updated the question. Commented Jan 23, 2017 at 10:22
  • What does this return var_dump((new finfo(FILEINFO_MIME_TYPE))->file($files['image']));? Commented Jan 23, 2017 at 10:32

1 Answer 1

1

Try to do this:

var_dump(v::image()->validate($_FILES['image']['tmp_name‌​']‌​));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.