Example #1
0
 public function actionAddAjax()
 {
     // 		[my-pic] => Array
     // 		(
     // 				[name] => 18348228424229089093.jpg
     // 				[type] => image/jpeg
     // 				[tmp_name] => Z:\tmp\php2F.tmp
     // 				[error] => 0
     // 				[size] => 24698
     // 		)
     // print_r($_GET);
     // print_r($_FILES);
     // 		print_r($_POST);
     // 		exit;
     Yii::import('application.vendors.*');
     require_once 'MyFunctions.php';
     $myFunc = new MyFunctions();
     if (!empty($_FILES['my-pic']) && (int) $_FILES['my-pic']['error'] == 0) {
         $img = $_FILES['my-pic'];
         $add_arr['date_add'] = time();
         $add_arr['file_name'] = $img['name'];
         $dir = "uploads/images/" . date('Y', $add_arr['date_add']) . "/" . date('m', $add_arr['date_add']) . "/";
         $myFunc->prepareDir($dir);
         $add_arr['file_name'] = $myFunc->getUnickFileName($dir, $add_arr['file_name']);
         $image = Yii::app()->image->load($img['tmp_name']);
         //$image->resize(400, 100)->rotate(-45)->quality(75)->sharpen(20);
         $image->save($dir . $add_arr['file_name']);
         $model = new Images();
         $add_arr['date_add'] = time();
         $add_arr['author_id'] = "1";
         $add_arr['date_edit'] = 0;
         $add_arr['title'] = 'testt';
         //Временно
         $add_arr['description'] = 'testd';
         //Временно
         if (!empty($_GET['page_id'])) {
             $add_arr['post_id'] = (int) $_GET['page_id'];
             $add_arr['note_type'] = "page";
         } elseif (!empty($_GET['post_id'])) {
             $add_arr['post_id'] = (int) $_GET['post_id'];
             $add_arr['note_type'] = "post";
         } else {
             echo "error_id";
             Yii::app()->end();
         }
         $model->attributes = $add_arr;
         if ($res = $model->save()) {
             echo "/" . $dir . $add_arr['file_name'];
             //echo "ok";
         } else {
             echo "not_saved";
             //print_r($res);
         }
         //print_r($_FILES);
     } else {
         echo "error_input_file";
     }
     Yii::app()->end();
 }