save() public method

主函数,验证并保存图片
public save ( )
Ejemplo n.º 1
0
 public function actionUpdate($id)
 {
     $model = $this->loadBiz(trim($id));
     $model->setScenario('sell');
     $this->performAjaxValidation($model);
     if (!empty($_POST)) {
         $model->attributes = $_POST['ARBiz'];
         //            dump($_FILES);
         //            控制器中使用实例如下:
         $upload = new FileUpload(array($model, 'license_photo'), 'upload/groupon');
         //model处理文件上传  $upload = new FileUpload(array($model,'pic'),'upload/goods');
         if (!$upload->isNull()) {
             if ($filename = $upload->save()) {
                 $model->license_photo = $filename;
                 //                      dump($filename);
             } else {
                 print_r($upload->getErrors());
                 throw new CHttpException('300', '文件上传失败');
             }
         }
         //            dump($model->attributes);
         if ($model->save()) {
             //                $return_url = $_POST['return_url'];
             $this->redirect(array('index'));
         }
     }
     $this->render('update', array('model' => $model));
 }
Ejemplo n.º 2
0
 public function actionUpdate($id)
 {
     $model = ARGroupon::model()->findByPk($id);
     if ($model == null) {
         $this->go('该商品不存在', Yii::app()->request->urlReferrer);
     }
     $model->setScenario('sell');
     $this->performAjaxValidation($model);
     if (!empty($_POST['ARGroupon'])) {
         //            dump($_FILES);
         //            dump($_POST);exit;
         $model->attributes = $_POST['ARGroupon'];
         if ($model->validate()) {
             $fileUpload = new FileUpload(array($model, 'image'), 'upload/groupon');
             if (!$fileUpload->isNull()) {
                 $filename = $fileUpload->save();
                 if ($filename) {
                     $model->image = $filename;
                 } else {
                     dump($fileUpload->getErrors());
                     throw new CHttpException(300, '图片上传出错');
                 }
             }
             $model->begin_time = strtotime($model->begin_time);
             $model->end_time = strtotime($model->end_time);
             $model->expire_time = strtotime($model->expire_time);
             if ($model->save()) {
                 Yii::app()->user->setFlash('success', '商品修改成功');
                 //                    $url = $_POST['return_url'];
                 //                    $this->go('商品修改成功', $url, 'success');
             }
         }
     }
     $this->render('update', array('model' => $model));
 }
 public function uploadFile()
 {
     $name = Input::get('name');
     $permission = Input::get('permisson');
     $id = Input::get("file_id");
     if ($id) {
         $record = FileUpload::find($id);
         if ($name && $record) {
             $record->name = $name;
             $record->read_permission = $permission != -1 ? $permission : null;
             if (Input::hasFile('fileUpload')) {
                 $file = Input::file('fileUpload');
                 $fileName = preg_replace("([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\).])", '', $name);
                 $fileName = preg_replace("([\\.]{2,})", '', $fileName);
                 $fileName .= "." . $file->getClientOriginalExtension();
                 $file->move(app_path() . "/uploads", $fileName);
                 $record->url = $fileName;
             }
             $record->save();
             return Redirect::to("/dashboard/storyteller/manage/files");
         } else {
             return Response::json(['success' => false, 'message' => 'Invalid data.']);
         }
     } else {
         if ($name && Input::hasFile('fileUpload')) {
             $file = Input::file('fileUpload');
             $newFile = new FileUpload();
             $newFile->name = $name;
             $newFile->read_permission = $permission != -1 ? $permission : null;
             $fileName = preg_replace("([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\).])", '', $name);
             $fileName = preg_replace("([\\.]{2,})", '', $fileName);
             $fileName .= "." . $file->getClientOriginalExtension();
             $file->move(app_path() . "/uploads", $fileName);
             $newFile->url = $fileName;
             $newFile->created_by = Auth::user()->id;
             $newFile->save();
             return Redirect::to("/dashboard/storyteller/manage/files");
         } else {
             return Response::json(['success' => false, 'message' => 'Invalid data.']);
         }
     }
 }
Ejemplo n.º 4
0
 public function actionUpload()
 {
     Yii::import("xupload.models.XUploadForm");
     //Here we define the paths where the files will be stored temporarily
     $path = Yii::app()->getBasePath() . "/../";
     $publicPath = 'http://img.yincart.com';
     //
     //	if (!file_exists($path)) {
     //	    mkdir($path, 0777, true);
     //	}
     //	$ymd = date("Ymd");
     //	$path .= $ymd . '/';
     //	if (!file_exists($path)) {
     //	    mkdir($path, 0777, true);
     //	}
     //This is for IE which doens't handle 'Content-type: application/json' correctly
     header('Vary: Accept');
     if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) {
         header('Content-type: application/json');
     } else {
         header('Content-type: text/plain');
     }
     //Here we check if we are deleting and uploaded file
     if (isset($_GET["_method"])) {
         if ($_GET["_method"] == "delete") {
             if ($_GET["file"][0] !== '.') {
                 $file = $path . $_GET["file"];
                 if (is_file($file)) {
                     unlink($file);
                 }
             }
             echo json_encode(true);
         }
     } else {
         $model = new XUploadForm();
         $upload = new FileUpload(array($model, 'file'), 'upload/groupon');
         if (!$upload->isNull()) {
             $model->mime_type = $upload->instance->getType();
             $model->size = $upload->instance->getSize();
             $model->name = $upload->instance->getName();
             if ($model->validate()) {
                 $filename = $upload->save();
                 //                }
                 //            }
                 //	    $model->file = CUploadedFile::getInstance($model, 'file');
                 //We check that the file was successfully uploaded
                 //	    if ($model->file !== null) {
                 //Grab some data
                 //		$model->mime_type = $model->file->getType();
                 //		$model->size = $model->file->getSize();
                 //		$model->name = $model->file->getName();
                 //(optional) Generate a random name for our file
                 //		$filename = date("YmdHis") . '_' . rand(10000, 99999);
                 //		$filename .= "." . $model->file->getExtensionName();
                 //		$filename =  $ymd . '/' . $filename;
                 //		if ($model->validate()) {
                 //Move our file to our temporary dir
                 //		    $model->file->saveAs($path . $filename);
                 //		    chmod($path . $filename, 0777);
                 //here you can also generate the image versions you need
                 //using something like PHPThumb
                 //Now we need to save this path to the user's session
                 if (Yii::app()->user->hasState('images')) {
                     $userImages = Yii::app()->user->getState('images');
                 } else {
                     $userImages = array();
                 }
                 $userImages[] = array("path" => $path . $filename, "thumb" => $path . $filename, "filename" => $filename, "url" => $publicPath . '/' . $filename, 'size' => $model->size, 'mime' => $model->mime_type, 'name' => $model->name);
                 Yii::app()->user->setState('images', $userImages);
                 //Now we need to tell our widget that the upload was succesfull
                 //We do so, using the json structure defined in
                 // https://github.com/blueimp/jQuery-File-Upload/wiki/Setup
                 echo json_encode(array(array("name" => $model->name, "type" => $model->mime_type, "size" => $model->size, "url" => $publicPath . '/' . $filename, "thumbnail_url" => $publicPath . '/' . "{$filename}", "delete_url" => $this->createUrl("upload", array("_method" => "delete", "file" => $filename)), "delete_type" => "POST")));
             } else {
                 //If the upload failed for some reason we log some data and let the widget know
                 echo json_encode(array(array("error" => $model->getErrors('file'))));
                 Yii::log("XUploadAction: " . CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction");
             }
         } else {
             throw new CHttpException(500, "Could not upload file");
         }
     }
 }