/** * actionUpload * * @access public * @return void */ public function actionUpload() { $rs = array('status' => 0, 'msg' => '', 'data' => array()); //ini_set('post_max_size','1024M'); //ini_set('upload_max_filesize','1024M'); //set_time_limit(0); if (is_array($_FILES) && count($_FILES)) { foreach ($_FILES as $k1 => $v1) { if ($v1['error'] == 0) { $ext = ''; if (($pos = strrpos($v1['name'], '.')) !== false) { $ext = strtolower((string) substr($v1['name'], $pos + 1)); } $path = 'upload/item/image/' . date("Ymd"); YcFileHelper::mkdir($path); $data_pic = date('YmdHis', time()) . '_' . md5(YcStringHelper::randString()) . '.' . $ext; $data_url = date("Ymd") . '/' . $data_pic; $data = $path . '/' . $data_pic; $mv = move_uploaded_file($v1['tmp_name'], Yii::getPathOfAlias("root") . '/' . $data); if ($mv) { $index = 0; if (!empty($_POST['item_id'])) { //找到最大排序的图片 $criteria = new CDbCriteria(); $criteria->compare('item_id', $_POST['item_id']); $criteria->order = 'position DESC'; $itemImgTmp = ItemImg::model()->find($criteria); if (!empty($itemImgTmp)) { $index = $itemImgTmp->position + 1; } } $itemImg = new ItemImg(); $itemImg->item_id = empty($_POST['item_id']) ? '' : $_POST['item_id']; $itemImg->url = $data_url; $itemImg->position = $index; $itemImg->create_time = time(); if ($itemImg->save()) { $rs = array('status' => 1, 'msg' => '', 'data' => array('img_id' => $itemImg->img_id, 'url' => YcImageHelper::getImageUrl($data))); } } else { $rs['msg'] = '保存文件时出错'; } } } } echo YcStringHelper::jsonEncode($rs); }
/** * add images * @throws Exception * @author milkyway(yhxxlm@gmail.com) */ public function addImages() { //If we have pending images if (Yii::app()->user->hasState('images')) { $userImages = Yii::app()->user->getState('images'); //Resolve the final path for our images // $path = Yii::app()->getBasePath() . "/../images/uploads/"; // $path = realpath(Yii::app()->getBasePath() . "/../upload/item/image") . "/"; // //Create the folder and give permissions if it doesnt exists // if (!is_dir($path)) { // mkdir($path); // chmod($path, 0777); // } //Now lets create the corresponding models and move the files foreach ($userImages as $k => $image) { if (is_file($image["path"])) { // if (rename($image["path"], $path . $image["url"])) { // chmod($path . $image["filename"], 0777); $img = new ItemImg(); // $img->size = $image["size"]; // $img->mime = $image["mime"]; // $img->name = $image["name"]; $img->url = $image["url"]; $img->item_id = $this->item_id; $img->store_id = $_SESSION['store']['store_id'] ? $_SESSION['store']['store_id'] : 0; $img->position = $k; $img->create_time = time(); 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('images', null); } }