Hi ..... my code not validate file extension and all test upload say "Field file must not be empty"
Model :
use Phalcon\Validation;
use Phalcon\Validation\Validator\Digit as DigitValidator;
use Phalcon\Validation\Validator\File as FileValidator;
class Post extends \Phalcon\Mvc\Model
{
public function validation()
{
$validator = new Validation();
$validator->add('title', new DigitValidator([
'message' => ':field must be numeric'
]));
$validator->add('file', new FileValidator([
'maxSize' => '2M',
'messageSize' => ':field exceeds the max filesize (:max)',
'allowedTypes' => array('image/jpeg', 'image/png'),
'messageType' => 'Allowed file types are :types',
'maxResolution' => '800x600',
'messageMaxResolution' => 'Max resolution of :field is :max'
]));
return $this->validate($validator);
}
}
Controller :
if ($this->request->hasFiles()) {
foreach ($this->request->getUploadedFiles() as $file) {
$file->moveTo(
"files/" . (new Random())->uuid() . "." . $file->getExtension()
);
/*if ($this->imageCheck($file->getRealType())) {
$file->moveTo(
"files/" . (new Random())->base64Safe() . "." . $file->getExtension()
);
}else {
throw new Exception("Invalid Extension", 1);
}*/
}
}