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; }
/** * Upload an image to be displayed on a page. * * @param array $files * * @throws \Exception * @return array */ public function uploadImage($files) { $imageValidator = new \Zend\Validator\File\IsImage(['magicFile' => false]); $extensionValidator = new \Zend\Validator\File\Extension(['JPEG', 'JPG', 'JFIF', 'TIFF', 'RIF', 'GIF', 'BMP', 'PNG']); if ($imageValidator->isValid($files['upload']['tmp_name'])) { if ($extensionValidator->isValid($files['upload'])) { $config = $this->getStorageConfig(); $fileName = $this->getFileStorageService()->storeUploadedFile($files['upload']); return $config['public_dir'] . '/' . $fileName; } else { throw new \Exception($this->getTranslator()->translate('The uploaded file does not have a valid extension')); } } else { throw new \Exception($this->getTranslator()->translate('The uploaded file is not a valid image')); } }
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())))); } }
public function upload($files, $album) { if (!$this->isAllowed('photo', 'upload')) { throw new \User\Permissions\NotAllowedException($this->getTranslator()->translate('Not allowed to upload photos.')); } $imageValidator = new \Zend\Validator\File\IsImage(array('magicFile' => false)); $extensionValidator = new \Zend\Validator\File\Extension(array('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($translator->translate('The uploaded file is not a valid image')); } }
/** * Upload an image to be displayed on a page. * * @param array $files * * @throws \Exception * @return array */ public function uploadImage($files) { $imageValidator = new \Zend\Validator\File\IsImage(array('magicFile' => false)); $extensionValidator = new \Zend\Validator\File\Extension(array('JPEG', 'JPG', 'JFIF', 'TIFF', 'RIF', 'GIF', 'BMP', 'PNG')); $translator = $this->getTranslator(); if ($files['upload']['error'] !== 0) { throw new \Exception($translator->translate('An unknown error occurred during uploading (' . $files['upload']['error'] . ')')); } if ($imageValidator->isValid($files['upload']['tmp_name'])) { if ($extensionValidator->isValid($files['upload']['name'])) { //TODO } else { throw new \Exception($translator->translate('The uploaded file does not have a valid extension')); } } else { throw new \Exception($translator->translate('The uploaded file is not a valid image')); } }