
我在使用thinkphp框架时,kindeditor编辑器的图片不能显示,今天认真研究了半天框架代码,终于找到了一个比较好的方法,自定义了一个方法,getStorageUrl(),下面结合onethink实例做一详细解说,绝对原创,并非抄袭
具体步骤:
第一步:在SAE的storage服务中创建一个domain,我这里创建的domain名为editor。没有使用经验的朋友,可以参考我即将发表创建domain的经验。
第二步:以onethink中topcomment挂载的插件Editor为例,涉及的文件:AddonsEditorControllerUploadController.class.php
第三步:sae上上传图片使用的驱动我们要改为Sae,即将UploadController.class.php类中的
upload方法中的代码:
setting为ApplicationHomeConfcofig.php中的'EDITOR_UPLOAD',看下图修改
/* 调用文件上传组件上传文件 */
$this->uploader = new Upload($setting,'Local');
修改为:
$this->uploader = new Upload($setting,'Sae');
这样修改后,上传时的驱动就变成了ThinkPHPLibraryThinkUploadDriverSae.class.php了,到现在我们就实现了图片上传到storage的目的,你可以到storage管理页面确认一下:http://sae.sina.com.cn/?m=storage&a=domain&app_id=用户名,需要登录sae,但是我们并没有解决掉图片路径回掉的问题。到这一步虽然图片上传成功但是在编辑器中却显示不出来,下面我参照ThinkPHPLibraryThinkUploadDriverSae.class.php的一些代码在AddonsEditorControllerUploadController.class.php类中加了getStorageUrl方法并对原有的upload方法做了进一步修改,具体看第四步的代码:
第四步:代码展示
/*
*sae下获取上传文件的storage的地址,
*
**/
public function getStorageUrl($root,$filename){
$arr=explode('/',trim($root,'./'));
$domain=strtolower(array_shift($arr));
$st=new SaeStorage();//用了SaeStorag中的API
return $st->getUrl($domain,$filename);
}
这是新增的方法,参考了Sae.class.php类的__construct方法和save方法,即图中标示代码的启发:
save方法
对upload方法做的进一步修改:
原有的代码:
现在的代码:
为了方便,我把代码也拷贝过来:
/* 上传图片 */
public function upload(){
session('upload_error', null);
/* 上传配置 */
$setting = C('EDITOR_UPLOAD');
/* 调用文件上传组件上传文件 */
$this->uploader = new Upload($setting, 'Sae');
$info = $this->uploader->upload($_FILES);
if($info){
//$url = C('EDITOR_UPLOAD.rootPath').$info['imgFile']['savepath'].$info['imgFile']['savename'];
$url = $info['imgFile']['savepath'].$info['imgFile']['savename'];
$url = str_replace('./', '/', $url);
$info['fullpath'] = $this->getStorageUrl($setting['rootPath'],$url);
//$info['fullpath'] = __ROOT__.$url;
}
session('upload_error', $this->uploader->getError());
return $info;
}
