save() private method

+---------------------------------------------------------- 上传一个文件 +---------------------------------------------------------- +----------------------------------------------------------
private save ( $file ) : string
return string +----------------------------------------------------------
コード例 #1
0
ファイル: WrapContractor.php プロジェクト: dmitryplus/gb
 public static function toBase($company_id = false)
 {
     if (isset($_POST['Contractor'])) {
         if (!$_POST['Contractor']['id']) {
             unset($_POST['Contractor']['id']);
         }
         if (!isset($_POST['Contractor']['company_id']) && $company_id) {
             $_POST['Contractor']['company_id'] = $company_id;
         }
         $arContractorCity = $_POST['Contractor']['city'];
         unset($_POST['Contractor']['city']);
         $contractor_id = SaveData::model('Contractor', $_POST['Contractor']);
         $logo_name = UploadFile::save('Contractor', $contractor_id, 'contractor-logo');
         $tContractor = Contractor::model()->findByPk($contractor_id);
         $tContractor->logo = $logo_name;
         $tContractor->update();
         /* сохраняем города */
         if ($arContractorCity) {
             foreach ($arContractorCity as $city_id) {
                 $arLink = array('city_id' => $city_id, 'contractor_id' => $contractor_id);
                 $link_id = SaveData::model('LinkCityContractor', $arLink);
             }
         }
         return $contractor_id;
     }
     return false;
 }
コード例 #2
0
ファイル: LessonController.php プロジェクト: stan5621/eduwind
 /**
  * 上传课时视频
  * Enter description here ...
  */
 public function actionUpload($lessonId)
 {
     $model = $this->loadModel($lessonId);
     if (isset($_FILES['file']['name'])) {
         $fileTypes = array('mp4', 'flv');
         // File extensions
         $fileParts = pathinfo($_FILES['file']['name']);
         if (in_array(strtolower($fileParts['extension']), $fileTypes)) {
             $tempFile = $_FILES['file']['tmp_name'];
             //向uploadfile表中插入记录
             $uploadFile = new UploadFile();
             $uploadFile->userId = Yii::app()->user->id;
             $uploadFile->addTime = time();
             $uploadFile->mime = CFileHelper::getMimeType($tempFile) ? CFileHelper::getMimeType($tempFile) : "video/mp4";
             $uploadFile->name = $_FILES['file']['name'];
             $uploadFile->size = $_FILES['file']['size'];
             $uploadFile->storage = 'local';
             $uploadFile->save();
             //得到id,并以此为文件名保存视频文件
             $id = $uploadFile->id;
             $path = 'uploads/uploadFile/Lesson/mediaId';
             if (!is_dir($path)) {
                 mkdir($path, 0777, true);
             }
             $path .= '/' . $id . "." . $fileParts['extension'];
             move_uploaded_file($tempFile, $path);
             $uploadFile->path = $path;
             $uploadFile->save();
             //清除旧数据
             $model->deleteMedia();
             $model->mediaType = "video";
             $model->mediaId = $uploadFile->getPrimaryKey();
             $model->save();
             //返回mediaId
             //	echo json_encode(array('id'=>$id,'status'=>'success'));
             //echo json_encode($uploadFile->name);
             echo true;
         } else {
             //echo json_encode(array('status'=>'fail'));
             //	echo json_encode(null);
             echo false;
         }
     }
     $this->layout = "/layouts/nonav_column1";
     $this->render('upload_fancy', array('model' => $model));
 }
コード例 #3
0
 /**
  * afterSave
  *
  * @param Model $model モデル
  * @param bool $created 新規作成
  * @param array $options オプション
  * @throws Exception
  * @return void
  */
 public function afterSave(Model $model, $created, $options = array())
 {
     foreach ($this->_settings[$model->alias]['fileFields'] as $fieldName => $filedOptions) {
         if (isset($model->data[$model->alias][$fieldName])) {
             $fileData = $model->data[$model->alias][$fieldName];
             if ($fileData['name']) {
                 $uploadFile = $this->UploadFile->create();
                 $pathInfo = pathinfo($fileData['name']);
                 $uploadFile['UploadFile']['plugin_key'] = Inflector::underscore($model->plugin);
                 $keyField = Hash::get($filedOptions, 'contentKeyFieldName', 'key');
                 $uploadFile['UploadFile']['content_key'] = $model->data[$model->alias][$keyField];
                 $uploadFile['UploadFile']['field_name'] = $fieldName;
                 $uploadFile['UploadFile']['original_name'] = $fileData['name'];
                 $uploadFile['UploadFile']['extension'] = $pathInfo['extension'];
                 $uploadFile['UploadFile']['real_file_name'] = $fileData;
                 // フィールド毎にオプションを設定しなおしてsave実行
                 $this->UploadFile->setOptions($filedOptions);
                 // ε(     v ゚ω゚) < 例外処理
                 $this->_uploadedFiles[$fieldName] = $this->UploadFile->save($uploadFile);
             }
         }
     }
     // アップロードがなかったら以前のデータを挿入する
     // formからhiddenで UploadFile.field_name.id 形式でデータが渡ってくる
     // $data['UploadFile']にはモデルデータ編集時に添付されてるファイルについてのデータが入っている
     $uploadFiles = Hash::get($model->data, 'UploadFile', array());
     foreach ($uploadFiles as $uploadFile) {
         // 同じfield_nameでアップロードされてるなら以前のファイルへの関連レコードを新規に追加する必要は無い(過去の関連レコードはそのまま)
         if (isset($this->_uploadedFiles[$uploadFile['field_name']])) {
             // 新たにアップロードされてる
             // 履歴のないモデル(is_latest, is_activeカラムがない)だったら、以前のファイルを削除する
             // 履歴のないモデルか?
             if (!$model->hasField('is_latest')) {
                 // 履歴をもたないモデルなら以前のファイルを削除
                 $this->UploadFile->removeFile($model->id, $uploadFile['id']);
             }
         } else {
             // 同じfield_nameでアップロードされてなければ以前のファイルへの関連レコードを入れる
             $removePath = $model->alias . '.' . $uploadFile['field_name'] . '.remove';
             if (Hash::get($model->data, $removePath, false)) {
                 // ファイル削除にチェックが入ってるのでリンクしない
                 // 今のコンテンツIDで関連テーブルのレコードがあったら、ユーザモデルのように履歴のないモデルなのでそのときは関連テーブルを消す必要があるのでremoveFileは呼んでおく。
                 $this->UploadFile->removeFile($model->id, $uploadFile['id']);
             } else {
                 $uploadFileId = $uploadFile['id'];
                 $this->_saveUploadFilesContent($model, $uploadFileId);
             }
         }
     }
     // 関連テーブルの挿入
     foreach ($this->_uploadedFiles as $uploadedFile) {
         $uploadFileId = $uploadedFile['UploadFile']['id'];
         $this->_saveUploadFilesContent($model, $uploadFileId);
     }
     $this->_uploadedFiles = array();
 }
コード例 #4
0
ファイル: index.php プロジェクト: sammychan1981/quanpin
 public function img_upload()
 {
     $path = APP_ROOT;
     $upf = new UploadFile('imgFile', $path, '2m', 'jpg,jpeg,gif');
     $upf->save();
     $info = $upf->getInfo();
     if ($info[0]['status'] == 1) {
         echo JSON::encode(array('error' => 0, 'url' => $info[0]['path']));
         exit;
     }
 }
コード例 #5
0
 public function actionUploadCallback()
 {
     //接收到文件名和key,向upload_file表中插入一条数据
     //echo json_encode(array('name'=>$_POST['name'],'key'=>$_POST['key']));
     $uploadFile = UploadFile::model()->findByAttributes(array('path' => $_POST['key']));
     if (!$uploadFile) {
         $uploadFile = new UploadFile();
     }
     $uploadFile->userId = $_POST['userId'];
     $uploadFile->addTime = time();
     $uploadFile->path = $_POST['key'];
     $uploadFile->mime = $_POST['mime'];
     $uploadFile->name = $_POST['name'];
     $uploadFile->size = $_POST['size'];
     $uploadFile->storage = 'cloud';
     $uploadFile->save();
     echo json_encode(array('id' => $uploadFile->id));
     //返回插入后的Id。
 }
コード例 #6
0
ファイル: admin.php プロジェクト: sammychan1981/quanpin
 function photoshop_upload()
 {
     $file = $_FILES['upfile'];
     if ($file['error'] == 4) {
         $msg = array('error', '请选择文件后再上传!');
         $this->redirect("photoshop", true, array('msg' => $msg));
     } else {
         if ($file['error'] == 1) {
             $msg = array('error', '文件超出了php.ini文件指定大小!');
             $this->redirect("photoshop", true, array('msg' => $msg));
         } else {
             if ($file['size'] > 0) {
                 $key = md5_file($file['tmp_name']);
                 $gallery = new Model('gallery');
                 $img = $gallery->where("`key`='" . $key . "'")->find();
                 echo JS::import('dialog?skin=brief');
                 echo JS::import('dialogtools');
                 if (!$img) {
                     $upfile_path = Tiny::getPath("uploads");
                     $upfile_url = preg_replace("|^" . APP_URL . "|", '', Tiny::getPath("uploads_url"));
                     $upfile = new UploadFile('upfile', $upfile_path, '10m');
                     $upfile->save();
                     $info = $upfile->getInfo();
                     $result = array();
                     if ($info[0]['status'] == 1) {
                         $url = $upfile_url . $info[0]['path'];
                         $key = md5_file($upfile_path . $info[0]['path']);
                         $type = Req::args("type") == null ? 0 : intval(Req::args('type'));
                         $gallery->data(array('key' => $key, 'type' => $type, 'img' => $url))->save();
                         echo "<script>art.dialog.opener.setImg('{$url}');</script>";
                     } else {
                         $msg = array('error', $info[0]['msg']);
                         $this->msg = $msg;
                         $this->redirect("photoshop", false);
                     }
                 } else {
                     $url = $img['img'];
                     echo "<script>art.dialog.opener.setImg('{$url}');</script>";
                 }
             }
         }
     }
     exit;
 }
コード例 #7
0
ファイル: ucenter.php プロジェクト: sammychan1981/quanpin
 public function upload_head()
 {
     $upfile_path = Tiny::getPath("uploads") . "/head/";
     $upfile_url = preg_replace("|" . APP_URL . "|", '', Tiny::getPath("uploads_url") . "head/", 1);
     //$upfile_url = strtr(Tiny::getPath("uploads_url")."head/",APP_URL,'');
     $upfile = new UploadFile('imgFile', $upfile_path, '500k', '', 'hash', $this->user['id']);
     $upfile->save();
     $info = $upfile->getInfo();
     $result = array();
     if ($info[0]['status'] == 1) {
         $result = array('error' => 0, 'url' => $upfile_url . $info[0]['path']);
         $image_url = $upfile_url . $info[0]['path'];
         $image = new Image();
         $image->suffix = '';
         $image->thumb(APP_ROOT . $image_url, 100, 100);
         $model = new Model('user');
         $model->data(array('head_pic' => $image_url))->where("id=" . $this->user['id'])->update();
         $safebox = Safebox::getInstance();
         $user = $this->user;
         $user['head_pic'] = $image_url;
         $safebox->set('user', $user);
     } else {
         $result = array('error' => 1, 'message' => $info[0]['msg']);
     }
     echo JSON::encode($result);
 }
コード例 #8
0
 /**
  * 保存一个附件
  * @param unknown_type $attribute
  * @throws CException
  */
 public function saveAttachment($attribute)
 {
     $file = AttachmentUploadedFile::getInstance($this->Owner, $attribute);
     if (!is_null($file)) {
         //			if(!$this->Owner->isNewRecord){
         //delete previous attachment
         //			if(file_exists($this->Owner->{$attribute})){
         $this->Owner->refresh();
         $uploadFile = UploadFile::model()->findByPk($this->Owner->{$attribute});
         if ($uploadFile) {
             $uploadFile->delete();
         }
         $this->Owner->isNewRecord = false;
         //unlink($this->Owner->{$attribute});
         //		}
         //			}else{
         //				$this->Owner->isNewRecord = false;
         //			}
         preg_match('/\\.(.*)$/', $file->name, $matches);
         $this->file_extension = end($matches);
         //检查后缀名是否符合
         if (!empty($this->items[$attribute]['exts']) && !in_array($this->file_extension, $this->items[$attribute]['exts'])) {
             return false;
         }
         $this->filename = $file->name;
         $path = $this->getParsedPath($attribute);
         preg_match('|^(.*[\\\\/])|', $path, $match);
         $folder = end($match);
         if (!is_dir($folder)) {
             mkdir($folder, 0777, true);
         }
         $file->saveAs($path, false);
         $file_type = filetype($path);
         $uploadFile = new UploadFile();
         $uploadFile->userId = Yii::app()->user->id;
         $uploadFile->addTime = time();
         $uploadFile->name = $file->name;
         //$uploadFile->mime  = mime_content_type($path);
         $uploadFile->mime = CFileHelper::getMimeType($path) ? CFileHelper::getMimeType($path) : "";
         $uploadFile->type = $this->file_extension;
         $uploadFile->size = filesize($path);
         $uploadFile->path = $path;
         $uploadFile->save();
         $this->Owner->saveAttributes(array($attribute => $uploadFile->id));
     }
     return true;
 }