sae中配置weiphp,修复图片上传问题

首先在Storage中新建domin:uploads;
然后修改以下文件

1、Sae.class.php 中修改为:  

    /** 保存指定文件
     * @param  array   $file    保存的文件信息
     * @param  boolean $replace 同名文件是否覆盖
     * @return boolean          保存状态,true-成功,false-失败
     */

    public function save(&$file, $replace=true) {
        $filename = ltrim($this->rootPath .'/'. $file['savepath'] . $file['savename'],'/');
        $st=new \SaeStorage();
        /* 不覆盖同名文件 */
        if (!$replace && $st->fileExists($this->domain,$filename)) {
            $this->error = '存在同名文件' . $file['savename'];
            return false;
        }

        /* 移动文件 */
        $url = $st->upload($this->domain,$filename,$file['tmp_name']);
        if (!$url) {
            $this->error = '文件上传保存错误!['.$st->errno().']:'.$st->errmsg();
            return false;
        } else {
            $file['url'= $url;
            $file['download'= $url;
        }

        return true;

    }




2、/Application/Admin/Conf/config.php 修改为:
PHP代码 
  1.   /* 图片上传相关配置 */
  2.   'PICTURE_UPLOAD' => array(
  3.     'mimes'  => ''//允许上传的文件MiMe类型
  4.     'maxSize'  => 2*1024*1024, //上传的文件大小限制 (0-不做限制)
  5.     'exts'  => 'jpg,gif,png,jpeg'//允许上传的文件后缀
  6.     'autoSub'  => true, //自动子目录保存文件
  7.     'subName'  => array('date''Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
  8.     'rootPath' => './Uploads/Picture/'//保存根路径
  9.     'savePath' => ''//保存路径
  10.     'saveName' => array('uniqid'''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
  11.     'saveExt'  => ''//文件保存后缀,空则使用原后缀
  12.     'replace'  => false, //存在同名是否覆盖
  13.     'hash'  => true, //是否生成hash编码
  14.     'callback' => false, //检测文件是否存在回调函数,如果存在返回文件信息数组
  15.   ), //图片上传相关配置(文件上传类配置)
  16.   //  'PICTURE_UPLOAD_DRIVER'=>'local',  
  17.   'PICTURE_UPLOAD_DRIVER'=>'Sae',
  18.   //SAE上传文件驱动配置
  19.   'UPLOAD_SAE_CONFIG'=>array(
  20.     'rootPath'=>'http://' . $_SERVER['HTTP_APPNAME'] . '-uploads.stor.sinaapp.com/Editor/',
  21.   'domain'=>'uploads',
  22. ),
  23.   
  24.   //本地上传文件驱动配置
  25.   'UPLOAD_LOCAL_CONFIG'=>array(),
  26.   'UPLOAD_BCS_CONFIG'=>array(
  27.     'AccessKey'=>'',
  28.     'SecretKey'=>'',
  29.     'bucket'=>'',
  30.     'rename'=>false
  31.   ),

3、/Application/Home/Conf/config.php 修改为:
  1. return array(
  2.     // 预先加载的标签库
  3.     'TAGLIB_PRE_LOAD'     =>    'OT\\TagLib\\Article,OT\\TagLib\\Think',
  4.         
  5.     /* 主题设置 */
  6.     'DEFAULT_THEME' =>  'default',  // 默认模板主题名称

  7.     /* SESSION 和 COOKIE 配置 */
  8.     'SESSION_PREFIX' => 'weiphp_home', //session前缀
  9.     'COOKIE_PREFIX'  => 'weiphp_home_', // Cookie前缀 避免冲突

  10.     /**
  11.      * 附件相关配置
  12.      * 附件是规划在插件中的,所以附件的配置暂时写到这里
  13.      * 后期会移动到数据库进行管理
  14.      */
  15.     'ATTACHMENT_DEFAULT' => array(
  16.         'is_upload'     => true,
  17.         'allow_type'    => '0,1,2', //允许的附件类型 (0-目录,1-外链,2-文件)
  18.         'driver'        => 'Sae', //上传驱动
  19.         'driver_config' => null, //驱动配置
  20.     ), //附件默认配置

  21.     'ATTACHMENT_UPLOAD' => array(
  22.         'mimes'    => '', //允许上传的文件MiMe类型
  23.         'maxSize'  => 5*1024*1024, //上传的文件大小限制 (0-不做限制)
  24.         'exts'     => 'jpg,gif,png,jpeg,zip,rar,tar,gz,7z,doc,docx,txt,xml', //允许上传的文件后缀
  25.         'autoSub'  => true, //自动子目录保存文件
  26.         'subName'  => array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
  27.         'rootPath' => './Uploads/Attachment/', //保存根路径
  28.         'savePath' => '', //保存路径
  29.         'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
  30.         'saveExt'  => '', //文件保存后缀,空则使用原后缀
  31.         'replace'  => false, //存在同名是否覆盖
  32.         'hash'     => true, //是否生成hash编码
  33.         'callback' => false, //检测文件是否存在回调函数,如果存在返回文件信息数组
  34.     ), //附件上传配置(文件上传类配置)
  35.     /* 模板相关配置 */
  36.     'TMPL_PARSE_STRING' => array(
  37.         '__STATIC__' => __ROOT__ . '/Public/static',
  38.         '__ADDONS__' => __ROOT__ . '/Public/' . MODULE_NAME . '/Addons',
  39.         '__IMG__'    => __ROOT__ . '/Public/' . MODULE_NAME . '/images',
  40.         '__CSS__'    => __ROOT__ . '/Public/' . MODULE_NAME . '/css',
  41.         '__JS__'     => __ROOT__ . '/Public/' . MODULE_NAME . '/js',
  42.     ),
  43.     /* 数据缓存设置 */
  44.     'DATA_CACHE_PREFIX'    => 'weiphp_', // 缓存前缀
  45.     'DATA_CACHE_TYPE'      => 'File', // 数据缓存类型
  46.     
  47.     /* 文件上传相关配置 */
  48.     'DOWNLOAD_UPLOAD' => array(
  49.         'mimes'    => '', //允许上传的文件MiMe类型
  50.         'maxSize'  => 5*1024*1024, //上传的文件大小限制 (0-不做限制)
  51.         'exts'     => 'jpg,gif,png,jpeg,zip,rar,tar,gz,7z,doc,docx,txt,xml', //允许上传的文件后缀
  52.         'autoSub'  => true, //自动子目录保存文件
  53.         'subName'  => array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
  54.         'rootPath' => './Uploads/Download/', //保存根路径
  55.         'savePath' => '', //保存路径
  56.         'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
  57.         'saveExt'  => '', //文件保存后缀,空则使用原后缀
  58.         'replace'  => false, //存在同名是否覆盖
  59.         'hash'     => true, //是否生成hash编码
  60.         'callback' => false, //检测文件是否存在回调函数,如果存在返回文件信息数组
  61.     ), //下载模型上传配置(文件上传类配置)    
  62.     
  63.     /* 图片上传相关配置 */
  64.     'PICTURE_UPLOAD' => array(
  65.         'mimes'    => '', //允许上传的文件MiMe类型
  66.         'maxSize'  => 2*1024*1024, //上传的文件大小限制 (0-不做限制)
  67.         'exts'     => 'jpg,gif,png,jpeg', //允许上传的文件后缀
  68.         'autoSub'  => true, //自动子目录保存文件
  69.         'subName'  => array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
  70.         'rootPath' => './Uploads/Picture/', //保存根路径
  71.         'savePath' => '', //保存路径
  72.         'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
  73.         'saveExt'  => '', //文件保存后缀,空则使用原后缀
  74.         'replace'  => false, //存在同名是否覆盖
  75.         'hash'     => true, //是否生成hash编码
  76.         'callback' => false, //检测文件是否存在回调函数,如果存在返回文件信息数组
  77.     ), //图片上传相关配置(文件上传类配置)

  78.     'PICTURE_UPLOAD_DRIVER'=>'Sae',    
  79.     
  80.     //本地上传文件驱动配置
  81.     'UPLOAD_LOCAL_CONFIG'=>array(),
  82.     'UPLOAD_BCS_CONFIG'=>array(
  83.         'AccessKey'=>'',
  84.         'SecretKey'=>'',
  85.         'bucket'=>'',
  86.         'rename'=>false
  87.     ),
  88.     'UPLOAD_QINIU_CONFIG'=>array(
  89.         'accessKey'=>'__ODsglZwwjRJNZHAu7vtcEf-zgIxdQAY-QqVrZD',
  90.         'secrectKey'=>'Z9-RahGtXhKeTUYy9WCnLbQ98ZuZ_paiaoBjByKv',
  91.         'bucket'=>'onethinktest',
  92.         'domain'=>'onethinktest.u.qiniudn.com',
  93.         'timeout'=>3600,
  94.     ),

  95.     /* 编辑器图片上传相关配置 */
  96.     'EDITOR_UPLOAD' => array(
  97.         'mimes'    => '', //允许上传的文件MiMe类型
  98.         'maxSize'  => 2*1024*1024, //上传的文件大小限制 (0-不做限制)
  99.         'exts'     => 'jpg,gif,png,jpeg', //允许上传的文件后缀
  100.         'autoSub'  => true, //自动子目录保存文件
  101.         'subName'  => array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
  102.         'rootPath' => './Uploads/Editor/', //保存根路径
  103.         'savePath' => '', //保存路径
  104.         'saveName' => array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
  105.         'saveExt'  => '', //文件保存后缀,空则使用原后缀
  106.         'replace'  => false, //存在同名是否覆盖
  107.         'hash'     => true, //是否生成hash编码
  108.         'callback' => false, //检测文件是否存在回调函数,如果存在返回文件信息数组
  109.     ),        
  110. );
4、图片保存的位置 Application\Home\Model\PictureModel.class.php 修改为:

  1. namespace Home\Model;
  2. use Think\Model;
  3. use Think\Upload;

  4. /**
  5. * 图片模型
  6. * 负责图片的上传
  7. */

  8. class PictureModel extends Model{
  9.     /**
  10.      * 自动完成
  11.      * @var array
  12.      */
  13.     protected $_auto = array(
  14.         array('status', 1, self::MODEL_INSERT),
  15.         array('create_time', NOW_TIME, self::MODEL_INSERT),
  16.     );

  17.     /**
  18.      * 文件上传
  19.      * @param  array  $files   要上传的文件列表(通常是$_FILES数组)
  20.      * @param  array  $setting 文件上传配置
  21.      * @param  string $driver  上传驱动名称
  22.      * @param  array  $config  上传驱动配置
  23.      * @return array           文件上传成功后的信息
  24.      */
  25.     public function upload($files, $setting, $driver = 'Local', $config = null){
  26.         /* 上传文件 */
  27.         $setting['callback'] = array($this, 'isFile');
  28.         $setting['removeTrash'] = array($this, 'removeTrash');
  29.         $Upload = new Upload($setting, $driver, $config);
  30.         $info   = $Upload->upload($files);

  31.         if($info){ //文件上传成功,记录文件信息
  32.             foreach ($info as $key => &$value) {
  33.                 /* 已经存在文件记录 */
  34.                 if(isset($value['id']) && is_numeric($value['id'])){
  35.                     continue;
  36.                 }

  37.                 /* 记录文件信息 */
  38.                 if (strtolower($driver) == "sae") {
  39.                     $value['path'] = $value['url'];
  40.                 } else {
  41.                     $value['path'] = substr($setting['rootPath'], 1).$value['savepath'].$value['savename'];    //在模板里的url路径
  42.                 }
  43.                 if($this->create($value) && ($id = $this->add())){
  44.                     $value['id'] = $id;
  45.                 } else {
  46.                     //TODO: 文件上传成功,但是记录文件信息失败,需记录日志
  47.                     unset($info[$key]);
  48.                 }
  49.             }
  50.             return $info; //文件上传成功
  51.         } else {
  52.             $this->error = $Upload->getError();
  53.             return false;
  54.         }
  55.     }

  56.     /**
  57.      * 下载指定文件
  58.      * @param  number  $root 文件存储根目录
  59.      * @param  integer $id   文件ID
  60.      * @param  string   $args     回调函数参数
  61.      * @return boolean       false-下载失败,否则输出下载文件
  62.      */
  63.     public function download($root, $id, $callback = null, $args = null){
  64.         /* 获取下载文件信息 */
  65.         $file = $this->find($id);
  66.         if(!$file){
  67.             $this->error = '不存在该文件!';
  68.             return false;
  69.         }

  70.         /* 下载文件 */
  71.         switch ($file['location']) {
  72.             case 0: //下载本地文件
  73.                 $file['rootpath'] = $root;
  74.                 return $this->downLocalFile($file, $callback, $args);
  75.             case 1: //TODO: 下载远程FTP文件
  76.                 break;
  77.             default:
  78.                 $this->error = '不支持的文件存储类型!';
  79.                 return false;

  80.         }

  81.     }

  82.     /**
  83.      * 检测当前上传的文件是否已经存在
  84.      * @param  array   $file 文件上传数组
  85.      * @return boolean       文件信息, false - 不存在该文件
  86.      */
  87.     public function isFile($file){
  88.         if(empty($file['md5'])){
  89.             throw new \Exception('缺少参数:md5');
  90.         }
  91.         /* 查找文件 */
  92.         $map = array('md5' => $file['md5'],'sha1'=>$file['sha1'],);
  93.         return $this->field(true)->where($map)->find();
  94.     }

  95.     /**
  96.      * 下载本地文件
  97.      * @param  array    $file     文件信息数组
  98.      * @param  callable $callback 下载回调函数,一般用于增加下载次数
  99.      * @param  string   $args     回调函数参数
  100.      * @return boolean            下载失败返回false
  101.      */
  102.     private function downLocalFile($file, $callback = null, $args = null){
  103.         if(is_file($file['rootpath'].$file['savepath'].$file['savename'])){
  104.             /* 调用回调函数新增下载数 */
  105.             is_callable($callback) && call_user_func($callback, $args);

  106.             /* 执行下载 */ //TODO: 大文件断点续传
  107.             header("Content-Description: File Transfer");
  108.             header('Content-type: ' . $file['type']);
  109.             header('Content-Length:' . $file['size']);
  110.             if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
  111.                 header('Content-Disposition: attachment; filename="' . rawurlencode($file['name']) . '"');
  112.             } else {
  113.                 header('Content-Disposition: attachment; filename="' . $file['name'] . '"');
  114.             }
  115.             readfile($file['rootpath'].$file['savepath'].$file['savename']);
  116.             exit;
  117.         } else {
  118.             $this->error = '文件已被删除!';
  119.             return false;
  120.         }
  121.     }

  122.     /**
  123.      * 清除数据库存在但本地不存在的数据
  124.      * @param $data
  125.      */
  126.     public function removeTrash($data){
  127.         $this->where(array('id'=>$data['id'],))->delete();
  128.     }

  129. }

5、图片取得的函数
Application\Common\Common\function.php x修改为:
  1. function get_cover_url($cover_id) {
  2.     if (empty ( $cover_id )) {
  3.         return '';
  4.     }

  5.     // modify by xiahy, 2014/04/11,将图片存在SAE的stor中,所以保存的是完整路径
  6.     // return SITE_URL . get_cover ( $cover_id, 'path' );
  7.     $coverPath = get_cover ( $cover_id, 'path' );
  8.     if (strstr($coverPath, 'http://') or  strstr($coverPath, 'https://')) {
  9.         return $coverPath;
  10.     } else {
  11.         return SITE_URL . $coverPath;
  12.     }
  13. }

6、修改: /Addons/EditorForAdmin/Controller/UploadController.class.php
修改: Local 为 Sae
修改: EDITOR_UPLOAD 为 UPLOAD_SAE_CONFIG


ps:

Sae.class.php 这个文件在哪

QQ截图20141030164647.png 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值