コード例 #1
0
ファイル: Articles.php プロジェクト: equa2k9/jazz
 protected function afterSave()
 {
     if (Yii::app()->user->hasState('files')) {
         $userImages = Yii::app()->user->getState('files');
         $path = Yii::app()->getBasePath() . "/../images/catalog/{$this->id}/";
         //Create the folder and give permissions if it doesnt exists
         if (!is_dir($path)) {
             mkdir($path);
             chmod($path, 0777);
         }
         if (!is_dir($path . "thumb/")) {
             mkdir($path . "thumb/");
             chmod($path . "thumb/", 0777);
         }
         //Now lets create the corresponding models and move the files
         foreach ($userImages as $image) {
             if (is_file($image["path"])) {
                 if (rename($image["path"], $path . $image["filename"]) && rename($image["thumb"], $path . "thumb/" . $image["filename"])) {
                     chmod($path . $image["filename"], 0777);
                     chmod($path . 'thumb/' . $image["filename"], 0777);
                     $img = new ArticlesPictures();
                     $img->size = $image["size"];
                     $img->mime = $image["mime"];
                     $img->name = $image["name"];
                     $img->thumb = "/images/catalog/{$this->id}/thumb/" . $image["filename"];
                     $img->picture = $image["filename"];
                     $img->source = "/images/catalog/{$this->id}/" . $image["filename"];
                     $img->article_id = $this->id;
                     if (!$img->save()) {
                         //Its always good to log something
                         Yii::log("Could not save Image:\n" . CVarDumper::dumpAsString($img->getErrors()), CLogger::LEVEL_ERROR);
                         //this exception will rollback the transaction
                         throw new Exception('Could not save Image');
                     }
                 }
             } else {
                 //You can also throw an execption here to rollback the transaction
                 Yii::log($image["path"] . " is not a file", CLogger::LEVEL_WARNING);
             }
         }
         //Clear the user's session
         Yii::app()->user->setState('files', null);
     }
     parent::afterSave();
 }
コード例 #2
0
ファイル: ArticlesController.php プロジェクト: equa2k9/jazz
 public function actionUpload()
 {
     Yii::import("xupload.models.XUploadForm");
     //Here we define the paths where the files will be stored temporarily
     $path = realpath(Yii::app()->getBasePath() . "/../images/catalog/tmp") . "/";
     $publicPath = Yii::app()->getBaseUrl() . "/images/catalog/tmp/";
     //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 (isset($_GET["file"]) && $_GET["file"][0] !== '.') {
                 $file = $path . $_GET["file"];
                 if (is_file($file)) {
                     unlink($file);
                 }
             } elseif ($_GET["id"]) {
                 $delImage = ArticlesPictures::model()->findByPk($_GET["id"]);
                 if ($delImage) {
                     if (is_file(realpath(Yii::app()->getBasePath() . '/..' . $delImage->source))) {
                         @unlink(realpath(Yii::app()->getBasePath() . '/..' . $delImage->source));
                         @unlink(realpath(Yii::app()->getBasePath() . '/..' . $delImage->thumb));
                         $delImage->delete(false);
                     }
                 }
             }
             echo json_encode(true);
         } elseif ($_GET["_method"] == "list") {
             $article_id = $_GET['id'];
             $objProductImages = ArticlesPictures::model()->findAllByAttributes(array('article_id' => $article_id));
             if ($objProductImages !== null) {
                 $arrProductImages = array();
                 foreach ($objProductImages as $objProductImage) {
                     $arrProductImages[] = array("name" => $objProductImage->name, "id" => $objProductImage->getPrimaryKey(), "type" => $objProductImage->mime, "size" => $objProductImage->size, "url" => $objProductImage->source, "thumbnail_url" => $objProductImage->thumb, "delete_url" => $this->createUrl("upload", array("_method" => "delete", "id" => $objProductImage->id)), "delete_type" => "GET");
                 }
                 echo json_encode($arrProductImages);
             }
         }
     } else {
         $model = new XUploadForm();
         $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 = md5(Yii::app()->user->id . microtime() . $model->name);
             $filename .= "." . $model->file->getExtensionName();
             if ($model->validate()) {
                 //Move our file to our temporary dir
                 $model->file->saveAs($path . $filename);
                 chmod($path . $filename, 0777);
                 $this->saveThumb($filename, $model->file->getExtensionName());
                 //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('files')) {
                     $userImages = Yii::app()->user->getState('files');
                 } else {
                     $userImages = array();
                 }
                 $userImages[] = array("path" => $path . $filename, "thumb" => $path . "thumb/" . $filename, "filename" => $filename, 'size' => $model->size, 'mime' => $model->mime_type, 'name' => $model->name);
                 Yii::app()->user->setState('files', $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 . "thumb/{$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");
         }
     }
 }