public function index09Action() { echo "<h3 style='color:red;font-weight:bold'>" . __METHOD__ . "</h3>"; $validator = new \Zend\Validator\File\IsImage(); $input = PUBLIC_PATH . "files/Penguins.jpg"; $input = PUBLIC_PATH . "files/test.txt"; if (!$validator->isValid($input)) { $message = $validator->getMessages(); echo current($message); } else { echo "ok"; } return false; }
public function upload($files, $album) { $this->checkUploadAllowed(); $imageValidator = new \Zend\Validator\File\IsImage(['magicFile' => false]); $extensionValidator = new \Zend\Validator\File\Extension(['JPEG', 'JPG', 'JFIF', 'TIFF', 'RIF', 'GIF', 'BMP', 'PNG']); $translator = $this->getTranslator(); if ($files['file']['error'] !== 0) { throw new \Exception($translator->translate('An unknown error occurred during uploading (' . $files['file']['error'] . ')')); } /** * We re-add the original extension so it can be preserved later on * when moving the file. */ $extension = explode('/', $files['file']['type'])[1]; $path = $files['file']['tmp_name'] . '.' . $extension; move_uploaded_file($files['file']['tmp_name'], $path); if ($imageValidator->isValid($path)) { if ($extensionValidator->isValid($path)) { $this->storeUploadedPhoto($path, $album, true); } else { throw new \Exception($translator->translate('The uploaded file does not have a valid extension')); } } else { throw new \Exception(sprintf($translator->translate("The uploaded file is not a valid image \nError: %s"), implode(',', array_values($imageValidator->getMessages())))); } }