On my project, i have a form with 3 inputs : 'file', 'name', 'fname'
.
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(),
'fname' => 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.