I use Slim 3 Framework on my project, and i have a form with 3 inputs : 'file', 'name', 'firstname'.
Assuming that $request has the data inputs of my form, to get the uploaded file, i use this code
$files = $request->getUploadedFiles();
if (empty($files['file'])) {
throw new Exception('Invalid image');
}
$newfile = $files['file'];
Then, to validate my input forms, i use the Respect\Validation Library
$validation = $this->validator->validate($request, [
'file' => v::file()->mimetype('application/pdf'),
'name' => v::stringType()->notEmpty()->length(2, 50)->alpha(),
'firstname' => v::stringType()->notEmpty()->length(2, 50)->alpha()
]);
if($validation->failed() {
//...
}
The fact is the file validation always fails :
var_dump($request->getParam('file')); //return NULL
var_dump($newfile); //return the following
$newfile content
object(Slim\Http\UploadedFile)#59 (8) {
["file"]=> string(14) "/tmp/php409Uje"
["name":protected]=> string(47) "Myfile.pdf"
["type":protected]=> string(15) "application/pdf"
["size":protected]=> int(1404476) ["error":protected]=> int(0)
["sapi":protected]=> bool(true) ["stream":protected]=> NULL
["moved":protected]=> bool(false)
}
So now I'm stuck. I really can't figure out how can i tell the validator to validate $newfile MIMETYPE for the 'file' input.
'file' => v::mimetype('application/pdf')- spielerdsfilereturns NULL since i need -i think- to get files with getUploadedFiles(), so the validation will always fail because NULL is notapplication/pdftype - user9161685$this->validator? or the code, where you intialize it in the$container? - spielerds$container['validator'] = function ($container) { return new App\Validation\Validator; };- user9161685