public function upload($params = array()) { if (!is_dir($params['destination_folder'])) { mkdir($params['destination_folder'], 0777, true); } $adapter = new Zend_File_Transfer_Adapter_Http(); $adapter->setDestination($params['destination_folder']); $adapter->setValidators($params['validators']); if ($adapter->getValidator('ImageSize')) { $adapter->getValidator('ImageSize')->setMessages(array('fileImageSizeWidthTooBig' => $this->_('Image too large, %spx maximum allowed.', '%maxwidth%'), 'fileImageSizeWidthTooSmall' => $this->_('Image not large enough, %spx minimum allowed.', '%minwidth%'), 'fileImageSizeHeightTooBig' => $this->_('Image too high, %spx maximum allowed.', '%maxheight%'), 'fileImageSizeHeightTooSmall' => $this->_('Image not high enough, %spx minimum allowed.', '%minheight%'), 'fileImageSizeNotDetected' => $this->_("The image size '%s' could not be detected.", '%value%'), 'fileImageSizeNotReadable' => $this->_("The image '%s' does not exist", '%value%'))); } if ($adapter->getValidator('Size')) { $adapter->getValidator('Size')->setMessages(array('fileSizeTooBig' => $this->_("Image too large, '%s' allowed.", '%max%'), 'fileSizeTooSmall' => $this->_("Image not large enough, '%s' allowed.", '%min%'), 'fileSizeNotFound' => $this->_("The image '%s' does not exist", '%value%'))); } if ($adapter->getValidator('Extension')) { $adapter->getValidator('Extension')->setMessages(array('fileExtensionFalse' => $this->_("Extension not allowed, '%s' only", '%extension%'), 'fileExtensionNotFound' => $this->_("The file '%s' does not exist", '%value%'))); } $files = $adapter->getFileInfo(); $return_file = ''; foreach ($files as $file => $info) { //Créé l'image sur le serveur if (!$adapter->isUploaded($file)) { throw new Exception($this->_('An error occurred during process. Please try again later.')); } else { if (!$adapter->isValid($file)) { if (count($adapter->getMessages()) == 1) { $erreur_message = $this->_('Error : <br/>'); } else { $erreur_message = $this->_('Errors : <br/>'); } foreach ($adapter->getMessages() as $message) { $erreur_message .= '- ' . $message . '<br/>'; } throw new Exception($erreur_message); } else { $new_name = uniqid("file_"); if (isset($params['uniq']) and $params['uniq'] == 1) { if (isset($params['desired_name'])) { $new_name = $params['desired_name']; } else { $format = pathinfo($info["name"], PATHINFO_EXTENSION); if (!in_array($format, array("png", "jpg", "jpeg", "gif"))) { $format = "jpg"; } $new_name = $params['uniq_prefix'] . uniqid() . ".{$format}"; } $new_pathname = $params['destination_folder'] . '/' . $new_name; $adapter->addFilter(new Zend_Filter_File_Rename(array('target' => $new_pathname, 'overwrite' => true))); } $adapter->receive($file); $return_file = $new_name; } } } return $return_file; }
public function uploadAction() { if (!empty($_FILES)) { try { $path = '/var/apps/iphone/certificates/'; $base_path = Core_Model_Directory::getBasePathTo($path); $filename = uniqid() . '.pem'; $app_id = $this->getRequest()->getParam('app_id'); if (!is_dir($base_path)) { mkdir($base_path, 0775, true); } $adapter = new Zend_File_Transfer_Adapter_Http(); $adapter->setDestination($base_path); $adapter->setValidators(array('Extension' => array('pem', 'case' => false))); $adapter->getValidator('Extension')->setMessages(array('fileExtensionFalse' => $this->_("Extension not allowed, \\'%s\\' only", '%extension%'))); $files = $adapter->getFileInfo(); foreach ($files as $file => $info) { if (!$adapter->isUploaded($file)) { throw new Exception($this->_('An error occurred during process. Please try again later.')); } else { if (!$adapter->isValid($file)) { if (count($adapter->getMessages()) == 1) { $erreur_message = $this->_('Error : <br/>'); } else { $erreur_message = $this->_('Errors : <br/>'); } foreach ($adapter->getMessages() as $message) { $erreur_message .= '- ' . $message . '<br/>'; } throw new Exception($erreur_message); } else { $adapter->addFilter(new Zend_Filter_File_Rename(array('target' => $base_path . $filename, 'overwrite' => true))); $adapter->receive($file); } } } $certificat = new Push_Model_Certificate(); $certificat->find(array('type' => 'ios', 'app_id' => $app_id)); if (!$certificat->getId()) { $certificat->setType('ios')->setAppId($app_id); } $certificat->setPath($path . $filename)->save(); $datas = array('success' => 1, 'files' => 'eeeee', 'message_success' => $this->_('Info successfully saved'), 'message_button' => 0, 'message_timeout' => 2); } catch (Exception $e) { $datas = array('error' => 1, 'message' => $e->getMessage()); } $this->getLayout()->setHtml(Zend_Json::encode($datas)); } }
/** * Upload plugin zip file. * * @param string $formName name of the form on the user interface * @param stdclass $json * @return array * * $tempFileName, * $tempFilePath, * $json */ function upload($formName, $json) { $rootPath = RM_Environment::getConnector()->getRootPath(); //1. upload zip - check if this file name a .zip extension //2. move zip to temp directory $tempFolderName = 'temp'; $tempFolderPath = $rootPath . DIRECTORY_SEPARATOR . 'RM' . DIRECTORY_SEPARATOR . 'userdata' . DIRECTORY_SEPARATOR . $tempFolderName; $validators = array(); $validators[] = new Zend_Validate_File_Extension('zip'); $adapter = new Zend_File_Transfer_Adapter_Http(); $adapter->setValidators($validators); try { $adapter->setDestination($tempFolderPath); } catch (Zend_File_Transfer_Exception $exception) { throw new RM_Exception($exception->getMessage()); } if (!$adapter->receive()) { $message = $this->_translate->_('Admin.Plugins.InstallMsg', 'UploadFailed'); $message .= '. ' . implode("; ", $adapter->getMessages()); throw new RM_Exception($message); } else { $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'UploadSuccess')); } //3. create new directory for a plugin $files = $adapter->getFileInfo(); $tempFileName = $files[$formName]['name']; $tempFilePath = $tempFolderPath . DIRECTORY_SEPARATOR . $tempFileName; return array($tempFileName, $tempFilePath, $json); }
/** * metod que realiza o upload e a persistencia do Arquivo */ protected function _upload($dto) { $configs = \Core_Registry::get('configs'); $upload = new \Zend_File_Transfer_Adapter_Http(); $files = $upload->getFileInfo(); $filesUp = array(); $return = array(); $error = false; foreach ($files as $file => $info) { $upload->setDestination($configs['upload']['material']); $upload->setValidators(array()); $upload->addValidator('Size', TRUE, array('max' => $configs['upload']['materialApoioMaxSize'], 'messages' => "O tamanho do arquivo é superior ao permitido. O tamanho permitido é 25MB.")); $upload->addValidator('ExcludeExtension', TRUE, array('dll', 'msi', 'phtml', 'phar', 'pyc', 'py', 'jar', 'bat', 'com', 'exe', 'pif', 'bin', 'sh', 'pl', 'php') + array('messages' => "Extensão do arquivo inválida.")); $upload->getValidator('Upload')->setMessages(array('fileUploadErrorNoFile' => "Arquivo não selecionado.")); if ($upload->isValid($file)) { $fileinfo = pathinfo($info['name']); $upload->receive($file); $filesUp[] = $upload->getFileName($file); $return[] = array('name' => $upload->getFileName($file)); } else { $error = $upload->getMessages(); break; } } if ($error) { if (count($filesUp)) { foreach ($filesUp as $file) { unlink($file); } } return array('errors' => $error); } return $return; }
/** * Upload zip srchive with all language files and config.ini * * @return array array($iso, $name) ISO code of uploaded language and language name on this language */ function upload() { //0. Create temp folder $folderName = self::_createTempFolderName(); //We create a name in a timestamp $rmConfig = new RM_Config(); $chmodOctal = intval($rmConfig->getValue('rm_config_chmod_value'), 8); $tempFolderPath = RM_Environment::getConnector()->getTempFolderPath() . DIRECTORY_SEPARATOR . $folderName; if (mkdir($tempFolderPath, $chmodOctal) == false) { throw new RM_Exception($this->_translate->_('Admin.System.Language', 'CantCreateTempFolder')); } //1. Upload zip file $validators = array(); $validators[] = new Zend_Validate_File_Extension('zip'); $adapter = new Zend_File_Transfer_Adapter_Http(); $adapter->setValidators($validators); try { $adapter->setDestination($tempFolderPath); } catch (Zend_File_Transfer_Exception $exception) { throw new RM_Exception($exception->getMessage()); } if (!$adapter->receive()) { $message = $this->_translate->_('Admin.System.Language', 'UploadFailed'); $message .= '. ' . implode("; ", $adapter->getMessages()); throw new RM_Exception($message); } //2. Unpack it $files = $adapter->getFileInfo(); $tempFileName = $files[self::$_formName]['name']; $tempFilePath = $tempFolderPath . DIRECTORY_SEPARATOR . $tempFileName; if (!extension_loaded('zlib')) { unlink($tempFilePath); RM_Filesystem::deleteFolder($tempFolderPath); throw new RM_Exception($this->_translate->_('Admin.System.Language', 'ZlibNotSupported')); } $zip = new PclZip($tempFilePath); $userDataFolder = RM_Environment::getConnector()->getCorePath() . DIRECTORY_SEPARATOR . 'userdata'; $result = $zip->extract(PCLZIP_OPT_PATH, $userDataFolder, PCLZIP_OPT_SET_CHMOD, $chmodOctal); unlink($tempFilePath); RM_Filesystem::deleteFolder($tempFolderPath); if (!$result) { throw new RM_Exception($this->_translate->_('Admin.System.Language', 'UnzipFailed')); } //3. Parse config.ini file $iniFilePath = $userDataFolder . DIRECTORY_SEPARATOR . self::$_configFilename; if (is_file($iniFilePath) == false) { RM_Filesystem::deleteFolder($tempFolderPath); throw new RM_Exception($this->_translate->_('Admin.System.Language', 'NoConfigIniFile')); } $parser = new RM_Language_Config_Parser(); try { $config = $parser->getConfig($iniFilePath); } catch (RM_Exception $e) { RM_Filesystem::deleteFolder($tempFolderPath); throw new RM_Exception($e->getMessage()); } //4. Get iso $iso = $config->iso; $name = $config->name; unlink($iniFilePath); return array($iso, $name); }
/** * Metdo responsavel por persistir os uploads * @param type $destino * @param type $thumb * @param type $validFile * @param type $invalidFile * @return type */ public static function upload($destino = 'anexoArtefato', $thumb = FALSE, $validFile = TRUE, $invalidFile = TRUE, $validImageSize = TRUE) { $configs = \Core_Registry::get('configs'); $upload = new \Zend_File_Transfer_Adapter_Http(); $files = $upload->getFileInfo(); $filesUp = array(); $return = array(); $error = false; $pasta = 'anexo-material'; if ($destino == 'anexoArtefato') { $pasta = 'anexo-artefato'; } $path = current(explode('application', __DIR__)) . 'data' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $pasta . DIRECTORY_SEPARATOR; foreach ($files as $file => $info) { $upload = new \Zend_File_Transfer_Adapter_Http(); $upload->setDestination($path); $upload->setValidators(array()); $upload->addValidator('Size', TRUE, array('max' => '100MB', 'messages' => "O tamanho do arquivo é superior ao permitido. O tamanho permitido é 100MB.")); self::_invalidFile($invalidFile, $upload); self::_validFile($validFile, $upload); self::_validImageSize($validImageSize, $upload); self::_getValidator($upload); if ($upload->isValid($file)) { $fileinfo = pathinfo($info['name']); $upload->receive($file); $filesUp[] = $upload->getFileName($file); $return[] = array('name' => $upload->getFileName($file), 'size' => $upload->getFileSize($file)); } else { $error = $upload->getMessages(); break; } } if ($error) { if (count($filesUp)) { foreach ($filesUp as $file) { unlink($file); } } return array('errors' => $error); } if ($thumb) { $pasta = current(explode('application', __DIR__)) . 'data' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'thumbs' . DIRECTORY_SEPARATOR; foreach ($filesUp as $endereco) { $fileinfo = pathinfo($endereco); $image = \WideImage::load($endereco); $image->resize(300, 300, 'outside')->crop('50% - 150', '50% - 150', 300, 300)->saveToFile($pasta . $fileinfo['filename'] . '_300_X_300.' . strtolower($fileinfo['extension'])); $image->resize(133, 89, 'outside')->crop('50% - 67', '50% - 45', 133, 89)->saveToFile($pasta . $fileinfo['filename'] . '_133_X_89.' . strtolower($fileinfo['extension'])); } } return $return; }