/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Person(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Person'])) { $model->attributes = $_POST['Person']; $model->job = 0; $model->birthday = CDateTimeParser::parse($model->birthday, 'dd/MM/yyyy'); $model->avatar = CUploadedFile::getInstance($model, 'avatar'); if ($model->avatar != null) { $avatar_name = 'image_' . $model->id . '_' . time() . '.' . $model->avatar->getExtensionName(); $model->avatar->saveAs(Yii::getPathOfAlias('webroot') . Person::M_IMAGES . $avatar_name); $image = Yii::app()->image->load(Yii::getPathOfAlias('webroot') . Person::M_IMAGES . $avatar_name); if (128 < $image->__get('width')) { $image->resize(128, 128, Image::HEIGHT); } $image->save(Yii::getPathOfAlias('webroot') . Person::M_THUMBNAIL . $avatar_name); // or $image->save('images/small.jpg'); $model->avatar = $avatar_name; } else { $model->avatar = Person::M_NOIMAGE; } if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'update' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = new BanersElements(); $root = BanersRubrics::getRoot(new BanersRubrics()); $catalog = $root->descendants()->findAll($root->id); $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); // set attributes from get if (isset($_GET['BanersElements'])) { $model->attributes = $_GET['BanersElements']; } if (isset($_POST['BanersElements'])) { $model->attributes = $_POST['BanersElements']; $model->imagefile = CUploadedFile::getInstance($model, 'imagefile'); if (isset($model->imagefile)) { $ext = pathinfo($model->imagefile); $model->image = $ext['extension']; } if ($model->save()) { if (isset($model->imagefile) && ($modelSettings = SiteModuleSettings::model()->find('site_module_id = 15'))) { //Загружаем картинку $filename = $model->id . '.' . $model->image; $filepatch = '/../uploads/filestorage/baners/elements/'; $model->imagefile->saveAs(YiiBase::getPathOfAlias('webroot') . $filepatch . $filename); //Обработка изображения SiteModuleSettings::model()->chgImgModel($modelSettings, 'GD', 2, $model->id); } $url = isset($_POST['go_to_list']) ? $this->listUrl('index') : $this->itemUrl('update', $model->id); $this->redirect($url); } } $this->render('update', array('model' => $model, 'root' => $root, 'catalog' => $catalog)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['KqxsNam'])) { $oldimage = $model->image; $model->attributes = $_POST['KqxsNam']; $model->image = CUploadedFile::getInstance($model, 'image'); if ($model->image != NULL) { $folder = 'upload/' . date('Ymd'); if (!file_exists($folder)) { mkdir($folder); } $model->image->saveAs($folder . '/' . $model->image); $model->image = $folder . '/' . $model->image; } else { $model->image = $oldimage; } if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('update', array('model' => $model)); }
public function actionUpdate() { $model = $this->loadModel($id); if (isset($_POST['Avatars'])) { $model->attributes = $_POST['Avatars']; $model->icon = CUploadedFile::getInstance($model, 'icon'); if ($model->icon) { $sourcePath = pathinfo($model->icon->getName()); $fileName = date('m-d') . Yii::app()->user->name . '.' . $sourcePath['extension']; $model->image = $fileName; } if ($model->save()) { //Если отмечен чекбокс «удалить файл» if ($model->del_img) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . Yii::app()->urlManager->baseUrl . '/images/' . $model->image)) { //удаляем файл unlink('./images/' . $model->image); $model->image = ''; } } //Если поле загрузки файла не было пустым, то if ($model->icon) { $file = './images/' . $fileName; $model->icon->saveAs($file); $image = Yii::app()->image->load($file); $image->resize(100, 100); $image->save(); } $this->redirect(array('update', 'id' => $model->id)); } } $this->render('update', array('model' => $model)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Scodobjventascaract'])) { $model->attributes = $_POST['Scodobjventascaract']; // Verificar si existe un archivo que actualizar en la creacion if (isset($_FILES) && $_FILES['Scodobjventascaract']['error']['imagen'] == 0) { // Obtenemos la instancia del documento $fDocumento = CUploadedFile::getInstance($model, 'imagen'); $sPathDocumento = $sPathFile . '/' . $model->id_cod_obj_venta . '/'; if (!file_exists($sPathDocumento)) { mkdir($sPathDocumento, 0777, true); } $fDocumento->saveAs($sPathDocumento . $fDocumento->getName()); $model->imagen = $fDocumento->getName(); // $this->refresh(); } if ($model->save()) { $this->redirect(array('scodobjventas/' . $model->id_cod_obj_venta)); } } $this->render('update', array('model' => $model)); }
public function actionUpdate($id) { $model = $this->loadModel($id); if (isset($_POST["{$this->modelName}"])) { $model->attributes = $_POST["{$this->modelName}"]; $newImage = isset($_FILES['Themes']) && $_FILES['Themes']['name']['upload_img']; if ($newImage) { // delete old image $model->delImage(); $model->scenario = 'upload'; } if ($model->validate()) { if ($newImage) { $model->upload = CUploadedFile::getInstance($model, 'upload_img'); $model->bg_image = md5(uniqid()) . '.' . $model->upload->extensionName; } if ($model->save()) { if ($newImage) { $model->upload->saveAs(Yii::getPathOfAlias($model->path) . '/' . $model->bg_image); Yii::app()->user->setFlash('success', tt('Image successfully added', 'themes')); } else { Yii::app()->user->setFlash('success', tc('Success')); } $this->refresh(); } } } $this->render('update', array('model' => $model)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Links'])) { $oldlogo = $model->logo; $model->attributes = $_POST['Links']; $model->update_uid = Yii::app()->user->id; $model->update_time = date('Y-m-d H:i:s'); $image = CUploadedFile::getInstance($model, 'logo'); if ($image) { $savename = Yii::app()->params['uploadPath'] . time() . mt_rand(1, 999) . '.' . $image->extensionName; $model->logo = '/' . $savename; if ($model->validate()) { $image->saveAs($savename); } if (file_exists($oldlogo)) { unlink($oldlogo); } } else { $model->logo = $oldlogo; } if ($model->save()) { //$this->redirect(array('view','id'=>$model->id)); Yii::app()->user->setFlash('success', '信息提交成功!'); } else { Yii::app()->user->setFlash('success', '信息提交失败!'); } } $this->render('update', array('model' => $model)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Article(); if (isset($_POST['Article'])) { $model->author_id = Yii::app()->user->id; $model->author = Yii::app()->user->nickname; $model->attributes = $_POST['Article']; $upimg = CUploadedFile::getInstance($model, 'img'); //获得一个CUploadedFile的实例 if (is_object($upimg) && get_class($upimg) === 'CUploadedFile') { $model->img = './up/cover/cover_' . time() . '_' . rand(0, 9999) . '.' . $upimg->extensionName; //定义文件保存的名称 } else { $model->img = './up/cover/noPic.jpg'; // 若果失败则应该是什么图片 } if ($model->save()) { if (is_object($upimg) && get_class($upimg) === 'CUploadedFile') { $upimg->saveAs($model->img); // 上传图片 } //添加成功时候的提示信息设置 /** *setFlash getFlash hasFlash 几个方法 */ //Yii::app()->user->setFlash('success','添加成功!'); //重定向 //$this->redirect('./index.php?r=article/index'); $this->redirect_message('新感觉发布成功!', 'success', '3', $this->createUrl('article/index')); } } //设置下拉默认属性 $model->type_id = 2; $this->render('create', array('model' => $model)); }
public function actionImport() { $this->title = 'Import Komponen'; $model = new Component(); if (isset($_POST['Component'])) { $model->file = CUploadedFile::getInstance($model, "file"); if (!$model->file) { Yii::app()->user->setFlash('error', 'Pastikan file telah diisi.'); $this->redirect('import'); } $filePath = Yii::getPathOfAlias('webroot') . "/imports/" . $model->file; if ($model->file->saveAs($filePath)) { $objPHPExcel = new PHPExcel(); $fields = array(array('name' => 'satker_code', 'col' => 0), array('name' => 'activity_code', 'col' => 1), array('name' => 'output_code', 'col' => 2), array('name' => 'suboutput_code', 'col' => 3), array('name' => 'code', 'col' => 4), array('name' => 'name', 'col' => 5)); $this->importExcelToMysql($filePath, $fields); } } //get component error lists $componentError = new ComponentError('search'); $componentError->unsetAttributes(); // clear any default values if (isset($_GET['ComponentError'])) { $componentError->attributes = $_GET['ComponentError']; } $this->render('import', array('model' => $model, 'componentError' => $componentError)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Banner'])) { $record_image = $model->image; $model->attributes = $_POST['Banner']; $image = CUploadedFile::getInstance($model, 'image'); if ($image) { $genName = 'uploaded_' . date('YmdHis'); $saveName = $genName; while (file_exists($this->upload_path . $saveName . '.' . $image->getExtensionName())) { $saveName = $genName . '-' . rand(0, 99); } $model->image = $saveName . '.' . $image->getExtensionName(); } if ($model->save()) { if ($image) { if (file_exists($this->upload_path . $record_image)) { @unlink($this->upload_path . $record_image); } $image->saveAs($this->upload_path . $model->image); } $this->redirect(array('index', 'id' => $model->banner_id)); } else { $model->image = $record_image; } } $this->render('update', array('model' => $model)); }
public function actionUpdate($barangayOfficialId) { $model = BarangayOfficials::model()->findByPk($barangayOfficialId); if (isset($_POST['BarangayOfficials'])) { $oldProfile = $model->profile_image; $model->attributes = $_POST['BarangayOfficials']; /*retrieve the uploaded file */ $uploadedFileObject = CUploadedFile::getInstance($model, 'profile_image'); if ($uploadedFileObject) { $newFilename = uniqid() . $uploadedFileObject->getName(); $ouputfile = Yii::getPathOfAlias("imageUploads") . '/' . $newFilename; $uploadedFileObject->saveAs($ouputfile); $model->profile_image = $newFilename; } else { $model->profile_image = $oldProfile; } $termFrom = sprintf("first day of %s %s", $_POST['term_from_month'], $_POST['term_from_year']); $termTo = sprintf("last day of %s %s", $_POST['term_to_month'], $_POST['term_to_year']); $termFrom = date("Y-m-d H:i:s", strtotime($termFrom)); $termTo = date("Y-m-d H:i:s", strtotime($termTo)); $model->term_from = $termFrom; $model->term_to = $termTo; /*save to model*/ if ($model->save()) { Yii::app()->user->setFlash("success", "<strong>Saved</strong> : New record saved"); $this->redirect(array("view", 'id' => $model->id)); } } $this->render('index', array('model' => $model)); }
public function actionUpdate($id) { $model = $this->loadModel($id); $file = new File(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Personnel']) && isset($_POST['File'])) { $model->attributes = $_POST['Personnel']; $file->attributes = $_POST['File']; $file->file = CUploadedFile::getInstance($file, 'file'); if ($file->file != null) { if (file_exists(Yii::app()->params['pathUpload'] . $model->pic) && $model->pic != 'noimage.jpg') { unlink(Yii::app()->params['pathUpload'] . $model->pic); } if ($file->file != null) { $filename = time() . '.' . $file->file->getExtensionName(); $file->file->saveAs(Yii::app()->params['pathUpload'] . $filename); $model->pic = $filename; } } if ($model->save()) { $this->redirect(array('view', 'id' => $model->personnel_id)); } } $this->render('update', array('model' => $model, 'file' => $file)); }
public function actionCreate1() { $model = new Files(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Files'])) { $model->attributes = $_POST['Files']; if ($model->save()) { $file = CUploadedFile::getInstance($model, 'originalname'); if (is_object($file) && get_class($file) === 'CUploadedFile') { $model->filename = $file; } else { $model->filename = ""; } if (is_object($file) && get_class($file) === 'CUploadedFile') { // again, if anything was uploaded and if we have db done then move the file from tmp to the right place $model->filename->saveAs(Yii::app()->basePath . '/files/' . $model->filename->name); //unlink($model->fileWithPath(Yii::app()->basePath . '/files/' . $oldfilename)); } //$this->redirect(array('view','id'=>$model->id)); exit; } } $this->render('create', array('model' => $model)); }
/** * ajax修改用户头像 */ public function actionAjaxAvatar() { //获取当前用户的model $model = $this->loadModel(); $old = $model->attributes; $oldPath = $old['avatar']; if ($_FILES['AdminUser']['error']['avatar'] == 0) { //上传图片有效 $model->avatar = CUploadedFile::getInstance($model, 'avatar'); $filename = explode(".", $_FILES['AdminUser']['name']['avatar']); $data = date("Y", time()) . '/' . date("m", time()) . '/' . date("d", time()) . '/'; $save_path = "./upload/{$data}"; //创建相应了、目录 if (!file_exists($save_path)) { $this->mkdirs($save_path); } $namef = md5(uniqid(rand(), true)) . '.' . $filename[1]; $fullPath = $data . $namef; $model->avatar->saveAs(Yii::app()->basePath . "/../upload/{$fullPath}"); @unlink(Yii::app()->basePath . "/../upload/{$oldPath}"); $model->avatar = $fullPath; } if ($model->save()) { $this->updateUserInfo(); $this->redirect(array('index')); } }
public function actionUpdate($id = null) { $model = Documents::model()->findByPk($id); $flag = 0; if (!empty($_POST['Documents'])) { $filename_old = $model->attributes['filename']; if (!empty(CUploadedFile::getInstance($model, 'filename')->name)) { $model->attributes = $_POST['Documents']; $model->filename = CUploadedFile::getInstance($model, 'filename'); $filename = $model->filename; $document = explode('.', $model->filename->name); $filenameType = $document[count($document) - 1]; $filenameName = md5(uniqid()) . '.' . $filenameType; $model->type = end($document); $model->size = $model->filename->size; $model->md5name = $filenameName; $model->filename = $document[count($document) - 2] . "." . $model->type; $filenames_path = Yii::getPathOfAlias('webroot') . '/upload/documents/' . $filenameName; $flag = 1; } else { $model->attributes = $_POST['Documents']; $model->filename = $filename_old; } $model->updated = time(); if ($model->save()) { Yii::app()->user->setFlash('success', translate('Cập nhật thành công.')); if ($flag == 1) { $filename->saveAs($filenames_path); } $this->redirect(PIUrl::createUrl('/admin/Documents/')); } } $dataCategories = categoriesDocuments::model()->getCategoriesDocument(); $this->render('update', array('model' => $model, 'dataCategory' => $dataCategories)); }
/** * Saving action for create and update. * If creation is successful, the browser will be redirected to the 'view' page. */ public function initSave($model) { $this->performAjaxValidation($model); if (isset($_POST['File'])) { $model->attributes = $_POST['File']; if ($model->isNewrecord) { $model->created_at = time(); } if (isset($_FILES['File']['name']['file']) && $_FILES['File']['name']['file'] != '') { $model->name = $_FILES['File']['name']['file']; $model->file = CUploadedFile::getInstance($model, 'file'); $model->size = $_FILES['File']['size']['file']; } else { $model->file = ''; } $model->updated_at = time(); $flash = $model->isNewRecord ? 'File successfully created' : 'File successfully updated'; if ($model->validate()) { if ($model->save()) { if ($model->file != '') { $dir = BASE_PATH . DIRECTORY_SEPARATOR . 'uploadFiles'; if (!is_dir($dir)) { mkdir($dir, 0777); } $file = $dir . '/' . $model->name; // if(!is_file($img)) $model->file->saveAs($file); } Yii::app()->user->setFlash('success', $flash); $this->redirect(array('file/view', 'id' => $model->id)); } } } }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['FriendLink'])) { $model->attributes = $_POST['FriendLink']; $FriendLink = FriendLink::model()->findByPk($id); $img = $_FILES['FriendLink']['name']['image']; if ($img !== '') { $img = CUploadedFile::getInstance($model, 'image'); $extensionName = explode('.', $img->getName()); $extensionName = $extensionName[count($extensionName) - 1]; $dir = dirname(Yii::app()->basePath) . '/upload/link/'; $img_src = $dir . md5(time()) . '.' . $extensionName; $img1 = md5(time()) . '.' . $extensionName; $model->image = $img1; } else { $model->image = $FriendLink->image; } //$model->cate_id = $_POST['Caigou']['cate_id']; if ($model->save()) { if ($img !== '') { @unlink(dirname(Yii::app()->basePath) . '/upload/link/' . $FriendLink->image); $img->saveAs($img_src); } $this->redirect(array('/cms/friendLink/admin')); } } $this->render('update', array('model' => $model)); }
protected function beforeValidate() { if (empty($this->instanceId) && empty($this->tmpId)) { $this->addError('instanceId', 'Не указан instanceId.'); } $file = CUploadedFile::getInstance($this, 'file'); if ($file) { foreach (self::$deniedExtensions as $ext) { if (strpos($file->getName(), '.' . $ext) !== false) { $this->addError('file', 'Файл имеет запрещенное расширение.'); return false; } } } $objectParameter = $this->getObjectParameter(); //если есть ограничение по типу файлов, то нужно добавить в валидатор if ($objectParameter && ($addParam = $objectParameter->getAdditionalParameter())) { $types = FileExtension::getExtensionsByType($addParam); $validators = $this->getValidators('file'); if (isset($validators[0]) && $validators[0] instanceof CFileValidator) { $validators[0]->types = $types; } } return parent::beforeValidate(); }
public static function saveOther(Apartment $ad) { if (ApartmentVideo::saveVideo($ad)) { $ad->panoramaFile = CUploadedFile::getInstance($ad, 'panoramaFile'); $ad->scenario = 'panorama'; if (!$ad->validate()) { return false; } } $city = ""; if (issetModule('location')) { $city .= $ad->locCountry ? $ad->locCountry->getStrByLang('name') : ""; $city .= $city && $ad->locCity ? ", " : ""; $city .= $ad->locCity ? $ad->locCity->getStrByLang('name') : ""; } else { $city = $ad->city ? $ad->city->getStrByLang('name') : ""; } // data if ($ad->address && $city && (param('useGoogleMap', 1) || param('useYandexMap', 1) || param('useOSMMap', 1))) { if (!$ad->lat && !$ad->lng) { # уже есть $coords = Geocoding::getCoordsByAddress($ad->address, $city); if (isset($coords['lat']) && isset($coords['lng'])) { $ad->lat = $coords['lat']; $ad->lng = $coords['lng']; } } } return true; }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Slider'])) { if (file_exists(Yii::app()->basePath . self::URLUPLOAD . $model->file_name)) { unlink(Yii::app()->basePath . self::URLUPLOAD . $model->file_name); } $model->attributes = $_POST['Slider']; $uploadedFile = CUploadedFile::getInstance($model, 'file_name'); $rnd = rand(0, 999999); $wkt = date('m-d-Y-h-i-s', time()); $fileName = "{$wkt}_{$rnd}_{$uploadedFile}"; $model->file_name = $fileName; $model->last_update = new CDbExpression('NOW()'); if ($model->save()) { if ($uploadedFile !== null) { $uploadedFile->saveAs(Yii::app()->basePath . self::URLUPLOAD . $model->file_name); } $this->redirect(array('view', 'id' => $model->id)); } } $this->render('update', array('model' => $model)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); $menu = $model->attributes; if (isset($_POST['FrontMenu'])) { $model->attributes = $_POST['FrontMenu']; foreach ($this->uploadArr as $column) { $file = CUploadedFile::getInstance($model, $column); //获取表单名为filename的上传信息 if ($file) { $model->{$column} = $this->uploadIcon($file); if ($menu[$column]) { $ftp = new Ftp(); $res = $ftp->delete_file('common/frontmenu/' . $menu[$column]); $ftp->close(); } } else { unset($model->{$column}); } } if (!$model->ParentID) { $model->ParentID = 0; } if ($model->save()) { $this->freshMenuCache(); $this->redirect(array('admin')); } } $this->render('update', array('model' => $model)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Partner'])) { $model->attributes = $_POST['Partner']; $model->fileLogo = CUploadedFile::getInstance($model, 'fileLogo'); if ($model->fileLogo) { if ($model->validate(array('fileLogo'))) { $fileName = $this->getAndSaveUploadedFile($model); if ($fileName) { if ($model->logo_path && file_exists($model->logo_path)) { unlink($model->logo_path); } $model->logo_path = $fileName; } } } if ($model->save()) { $this->redirect(array('admin')); } } $this->render('update', array('model' => $model)); }
public function beforeSave() { parent::beforeSave(); $picture = CUploadedFile::getInstance($this, 'image'); if ($picture) { $imagename = $picture->getTempName(); $image = Yii::app()->image->load($imagename); if ($image) { if ($this->avatar) { unlink($_SERVER['DOCUMENT_ROOT'] . $this->avatar_folder . '/' . $this->avatar); } if ($image->width >= $image->height) { $image->resize(20000, 93)->rotate(0)->quality(90)->sharpen(20); } else { $image->resize(93, 20000)->rotate(0)->quality(90)->sharpen(20); } $image->crop(93, 93); $file_name = rand() . '.' . $picture->extensionName; $savename = $_SERVER['DOCUMENT_ROOT'] . $this->avatar_folder . '/' . $file_name; $image->save($savename); $this->avatar = $file_name; } } return true; }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Photo(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Photo'])) { $model->attributes = $_POST['Photo']; $picture_name = ''; $picture_file = CUploadedFile::getInstance($model, 'file'); $model->file = $picture_file; if ($picture_file) { $picture_name = $picture_file->name; if (!is_dir(Yii::getPathOfAlias('webroot') . '/themes/gallery-images/')) { mkdir(Yii::getPathOfAlias('webroot') . '/themes/gallery-images/'); } if (!is_dir(Yii::getPathOfAlias('webroot') . '/themes/gallery-images/')) { mkdir(Yii::getPathOfAlias('webroot') . '/themes/gallery-images/'); $picture_file->SaveAs(Yii::getPathOfAlias('webroot') . '/themes/gallery-images' . $picture_file->getName()); } else { $picture_file->SaveAs(Yii::getPathOfAlias('webroot') . '/themes/gallery-images' . $picture_file->getName()); } } if ($model->save()) { $this->redirect(array('view', 'id' => $model->photo_id)); } } $this->render('create', array('model' => $model)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. */ public function actionUpdate() { $model = $this->loadModel(); if (isset($_POST['Post'])) { $model->attributes = $_POST['Post']; $prefix = rand(0, 99); // generate random number between 0-99 $uploadedFile = CUploadedFile::getInstance($model, 'icon'); $sourcePath = Yii::app()->basePath . '/../images/uploads/'; $fileName = "{$prefix}-{$uploadedFile}"; $model->image = $fileName; if ($model->save()) { /* * uncomment this field for the activate checkbox for remove image * */ /*if($model->del_img) { if(file_exists(Yii::getPathOfAlias('webroot').'/images/uploads/'.$model->image)) { unlink(Yii::getPathOfAlias('webroot').'/images/uploads/'.$model->image); } }*/ $uploadedFile->saveAs($sourcePath . $fileName); // image will upload to rootDirectory/images/uploads/ $model->icon = $fileName; } $this->redirect(array('view', 'id' => $model->id)); } $this->render('update', array('model' => $model)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new MCompany(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['MCompany'])) { $model->attributes = $_POST['MCompany']; $brandImage = CUploadedFile::getInstance($model, 'image_id'); if ($brandImage instanceof CUploadedFile) { $image = new MImage(); $path_parts = pathinfo($brandImage->getName()); $file_name = time() . "." . $path_parts['extension']; $path = Yii::app()->storagePath . "brand" . DIRECTORY_SEPARATOR . $file_name; $brandImage->saveAs($path); $image->created = time(); $image->path = "/storage/brand/" . $file_name; $image->created_by = Yii::app()->user->id; $image->save(false); } $model->image_id = $image->id; $model->created = time(); $model->created_by = Yii::app()->user->id; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public function actionUpdate($id) { $model = $this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $oldFileName = $model->picture; if (isset($_POST['News'])) { $model->attributes = $_POST['News']; $uploader = CUploadedFile::getInstance($model, 'picture'); if ($uploader) { $sourcePath = pathinfo($uploader->getName()); $md5_name = md5(date('H:i:s')); $fileName = $md5_name . '.' . $sourcePath['extension']; //generate new filename $model->picture = $fileName; } if ($model->save()) { if ($uploader) { @unlink(ROOT_DIR . '/images/news/' . $oldFileName); @unlink(ROOT_DIR . '/images/news/thumb/' . $oldFileName); $uploader->saveAs(ROOT_DIR . '/images/news/' . $fileName); // $this->saveThumb($md5_name, $sourcePath['extension']); } $this->redirect($this->createUrl('index')); } } $this->render('update', array('model' => $model)); }
public function actionUpload() { $cs = Yii::app()->getClientScript(); $cs->registerCoreScript('jquery'); $model = new Image(); if (isset($_FILES['Image'])) { $model->image = CUploadedFile::getInstance($model, 'image'); if ($model->validate()) { $name = $model->image->name; if (file_exists(Yii::app()->params['imageHomeAbs'] . $name)) { // already there $v = 2; preg_match('/(\\w+)\\.(\\w+)/', $name, $match); do { $name = $match[1] . '(' . $v++ . ').' . $match[2]; } while (file_exists(Yii::app()->params['imageHomeAbs'] . $name)); } if ($model->validate()) { $model->image->saveAs(Yii::app()->params['imageHomeAbs'] . $name); } } } // directory search $current = Yii::app()->params['imageHomeAbs']; $filelist = array(); $d = dir($current); while ($tmp = $d->read()) { if ($tmp != '.' && $tmp != '..' && $tmp != '.svn') { array_push($filelist, $tmp); } } asort($filelist, SORT_STRING); $this->render('gallery', array('model' => $model, 'filelist' => $filelist, 'current' => $current, 'cs' => $cs)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. */ public function actionEdit() { $model = $this->loadUser(); $profile = $model->profile; // ajax validator if (isset($_POST['ajax']) && $_POST['ajax'] === 'profile-form') { echo UActiveForm::validate(array($model, $profile)); Yii::app()->end(); } if (isset($_POST['User'])) { $model->attributes = $_POST['User']; $model->image = CUploadedFile::getInstance($model, 'image'); $profile->attributes = $_POST['Profile']; if ($model->validate() && $profile->validate()) { $model->save(); $profile->save(); Yii::app()->user->updateSession(); Yii::app()->user->setFlash('profileMessage', Yii::t('main', "Data saved successfully!")); $this->redirect(array('/user/profile')); } else { $profile->validate(); } } $this->render('edit', array('model' => $model, 'profile' => $profile)); }
/** * @param $value * @param $model * @param $field_varname * @return string */ public function setAttributes($value, $model, $field_varname) { $value = CUploadedFile::getInstance($model, $field_varname); if ($value) { $old_file = $model->getAttribute($field_varname); $file_name = ROOT_DIR . $this->params['path'] . '/' . $value->name; if (file_exists($file_name)) { $file_name = str_replace('.' . $value->extensionName, '-' . time() . '.' . $value->extensionName, $file_name); } if ($model->validate()) { if ($old_file && file_exists($old_file)) { unlink($old_file); } $file_name = str_replace(ROOT_DIR, '', $file_name); $value->saveAs($file_name); } $value = $file_name; } else { if (isset($_POST[get_class($model)]['uwfdel'][$field_varname]) && $_POST[get_class($model)]['uwfdel'][$field_varname]) { $old_file = $model->getAttribute($field_varname); if ($old_file && file_exists($old_file)) { unlink($old_file); } $value = ''; } else { $value = $model->getAttribute($field_varname); } } return $value; }