Ejemplo n.º 1
0
 /**
  * 上传图片
  * @author huajie <*****@*****.**>
  */
 public function uploadPicture()
 {
     //TODO: 用户登录检测
     /* 返回标准数据 */
     $return = array('status' => 1, 'info' => '上传成功', 'data' => '');
     /* 调用文件上传组件上传文件 */
     $Picture = D('Picture');
     $driver = modC('PICTURE_UPLOAD_DRIVER', 'local', 'config');
     $driver = check_driver_is_exist($driver);
     $uploadConfig = get_upload_config($driver);
     $info = $Picture->upload($_FILES, C('PICTURE_UPLOAD'), $driver, $uploadConfig);
     //TODO:上传到远程服务器
     /* 记录图片信息 */
     if ($info) {
         $return['status'] = 1;
         empty($info['download']) && ($info['download'] = $info['file']);
         $return = array_merge($info['download'], $return);
     } else {
         $return['status'] = 0;
         $return['info'] = $Picture->getError();
     }
     /* 返回JSON数据 */
     $this->ajaxReturn($return);
 }
 public function uploadAvatar()
 {
     $aUid = I('get.uid', 0, 'intval');
     mkdir("./Uploads/Avatar/" . $aUid);
     $files = $_FILES;
     $setting = C('PICTURE_UPLOAD');
     $driver = modC('PICTURE_UPLOAD_DRIVER', 'local', 'config');
     $driver = check_driver_is_exist($driver);
     $uploadConfig = get_upload_config($driver);
     /* 上传文件 */
     $setting['rootPath'] = './Uploads/Avatar';
     $setting['saveName'] = array('uniqid', '/' . $aUid . '/');
     $setting['savepath'] = '';
     $setting['subName'] = '';
     $setting['replace'] = true;
     //sae下
     if (strtolower(C('PICTURE_UPLOAD_DRIVER')) == 'sae') {
         // $config[]
         C(require_once APP_PATH . 'Common/Conf/config_sae.php');
         $Upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), array(C('UPLOAD_SAE_CONFIG')));
         $info = $Upload->upload($files);
         $config = C('UPLOAD_SAE_CONFIG');
         if ($info) {
             //文件上传成功,记录文件信息
             foreach ($info as $key => &$value) {
                 $value['path'] = $config['rootPath'] . 'Avatar/' . $value['savepath'] . $value['savename'];
                 //在模板里的url路径
             }
             /* 设置文件保存位置 */
             $this->_auto[] = array('location', 'Ftp' === $driver ? 1 : 0, self::MODEL_INSERT);
         }
     } else {
         $Upload = new \Think\Upload($setting, $driver, $uploadConfig);
         $info = $Upload->upload($files);
     }
     if ($info) {
         //文件上传成功,不记录文件
         $return['status'] = 1;
         if ($info['Filedata']) {
             $return = array_merge($info['Filedata'], $return);
         }
         if ($info['download']) {
             $return = array_merge($info['download'], $return);
         }
         /*适用于自动表单的图片上传方式*/
         if ($info['file']) {
             $return['data']['file'] = $info['file'];
             $path = $info['file']['url'] ? $info['file']['url'] : "./Uploads/Avatar" . $info['file']['savename'];
             $src = $info['file']['url'] ? $info['file']['url'] : __ROOT__ . "/Uploads/Avatar" . $info['file']['savename'];
             // $return['data']['file']['path'] =;
             $return['data']['file']['path'] = $path;
             $return['data']['file']['src'] = $src;
             $size = getimagesize($path);
             $return['data']['file']['width'] = $size[0];
             $return['data']['file']['height'] = $size[1];
             $return['data']['file']['time'] = time();
         }
     } else {
         $return['status'] = 0;
         $return['info'] = $Upload->getError();
     }
     $this->ajaxReturn($return);
 }