public function actionIndex($is_product = 1) { if (!empty($_POST)) { $is_new_product = $is_product; $images = CUploadedFile::getInstancesByName('images'); if (isset($images) && count($images) > 0) { // go through each uploaded image foreach ($images as $image => $pic) { $model = new Slides(); $imageType = explode('.', $pic->name); $imageType = $imageType[count($imageType) - 1]; $imageName = md5(uniqid()) . '.' . $imageType; if ($pic->saveAs(Yii::getPathOfAlias('webroot') . '/upload/images/' . $imageName)) { $model->image = $imageName; $model->name = $pic->name; $model->is_product = $is_new_product; $model->save(); Yii::app()->user->setFlash('success', translate('Thêm thành công.')); } // handle the errors here, if you want } } PIUrl::createUrl('/admin/slides/index', array('is_product' => $is_product)); } $criteria = new CDbCriteria(); $criteria->addCondition("is_product= {$is_product}"); $criteria->order = 'id DESC'; $count = Slides::model()->count($criteria); $pages = new CPagination($count); // results per page $pages->pageSize = 6; $pages->applyLimit($criteria); $model = Slides::model()->findAll($criteria); $this->render('index', compact('model', 'pages')); }
public function post_upload() { $input = Input::all(); $rules = array('file' => 'image|max:3000'); $messages = array('image' => 'Todos los archivos deben ser imagenes', 'max' => 'Las imagenes deben ser de menos de 3Mb'); $validation = Validator::make($input, $rules, $messages); if ($validation->fails()) { return Response::make($validation)->withErrors($validation); } $images = new Slides(); $file = Input::file('file'); $tipo = Input::get('tipo'); if (file_exists('images/slides-top/' . $file->getClientOriginalName())) { //guardamos la imagen en public/imgs con el nombre original $i = 0; //indice para el while //separamos el nombre de la img y la extensión $info = explode(".", $file->getClientOriginalName()); //asignamos de nuevo el nombre de la imagen completo $miImg = $file->getClientOriginalName(); //mientras el archivo exista iteramos y aumentamos i while (file_exists('images/slides-top/' . $miImg)) { $i++; $miImg = $info[0] . "(" . $i . ")" . "." . $info[1]; } //guardamos la imagen con otro nombre ej foto(1).jpg || foto(2).jpg etc $file->move("images/slides-top/", $miImg); if ($miImg != $file->getClientOriginalName()) { $images->image = $miImg; } } else { $file->move("images/slides-top/", $file->getClientOriginalName()); $images->image = $file->getClientOriginalName(); } $images->tipo = $tipo; $images->save(); return Response::json(array('image' => $images->id)); }