该教程算不上是phpcmsv9阿里云oss插件,所以整个修改及其代码覆盖前请一定记得备份。还有一点就是后台发布文章时上传的附件还是会保存在你的服务器上,基于以下原因:
1、个人的需求是前台页面需要使用thumb函数生成多个缩略图大小,thumb函数是不支持远程图片的(保存在OSS上就变远程图片了),即使修改为支持远程图片,也需要将远程图片先保存到本地,这样速度会非常慢,是非常非常;
2、做备份的需要,这点你懂得,某天OSS上的附件没有了就悲剧了,所以使用这个OSS for phpcmsV9需要服务器有一定的空间。
3、当然你也可以修改该phpcmsv9 oss插件,云存储上传成功后,可删除本地文件。
下面是阿里云oss phpcmsv9整合教程:
1、下载OSS的SDK,下载地址
http://dev.aliyun.com/article/detail?spm=0.0.0.62.q5af2S&article_id=45
2、解压SDK压缩包,打开conf.inc.php,修改Access Key ID和Access Key Secret为自己的,如何获取API 密钥(Access ID & Access Key ),
3、将SDK包中的以下文件放入对应模块下。
4、打开phpcms\modules\admin\templates\setting.tpl.php 搜索
<td class="y-bg"><input type="text" class="input-text" name="setconfig[upload_url]" id="upload_url" size="50" value="<?php echo $upload_url?>" /></td>
在下方添加
<!--阿里云OSS配置 S-->
<tr>
<th width="120"><?php echo L('setting_oss_enable')?></th>
<td class="y-bg">
<input name="setconfig[oss_enable]" value="1" type="radio" <?php echo ($oss_enable=='1') ? ' checked' : ''?>> <?php echo L('setting_yes')?>
<input name="setconfig[oss_enable]" value="0" type="radio" <?php echo ($oss_enable=='0') ? ' checked' : ''?>> <?php echo L('setting_no')?></td>
</tr>
<tr>
<th width="120"><?php echo L('setting_oss_id')?></th>
<td class="y-bg">
<input type="text" class="input-text" name="setconfig[oss_id]" id="oss_id" size="50" value="<?php echo $oss_id?>" />
<div class="onShow">还没有?<a href="http://help.aliyun.com/manual?spm=0.0.0.111.8BPOua&helpId=786" target="_blank">到这里获取API 密钥</a></div>
</td>
</tr>
<tr>
<th width="120"><?php echo L('setting_oss_secret')?></th>
<td class="y-bg"><input type="password" class="input-text" name="setconfig[oss_secret]" id="oss_secret" size="50" value="<?php echo $oss_secret?>" /></td>
</tr>
<tr>
<th width="120"><?php echo L('setting_oss_bucket')?></th>
<td class="y-bg"><input type="text" class="input-text" name="setconfig[oss_bucket]" id="oss_bucket" size="50" value="<?php echo $oss_bucket?>" /></td>
</tr>
<tr>
<th width="120"><?php echo L('setting_oss_path')?></th>
<td class="y-bg"><input type="text" class="input-text" name="setconfig[oss_path]" id="oss_path" size="50" value="<?php echo $oss_path?>" /></td>
</tr>
<tr>
<th width="120"><?php echo L('setting_oss_upload_url')?></th>
<td class="y-bg"><input type="text" class="input-text" name="setconfig[oss_upload_url]" id="oss_upload_url" size="50" value="<?php echo $oss_upload_url?>" /></td>
</tr>
<!--阿里云OSS配置 E-->
打开phpcms\languages\zh-cn\admin.lang.php
在结束前加入
$LANG['setting_oss_enable'] = '启用阿里云存储';
$LANG['setting_oss_id'] = 'Access Key ID';
$LANG['setting_oss_secret'] = 'Access Key Secret';
$LANG['setting_oss_bucket'] = 'Bucket名称';
$LANG['setting_oss_path'] = 'OSS上的图片目录';
$LANG['setting_oss_upload_url'] = '访问路径';
打开phpcms\modules\admin\setting.php,
搜索
$setting['errorlog_size'] = trim($_POST['setting']['errorlog_size']);
在下方添加
//阿里云云存储配置
$setting['oss_enable'] = intval($_POST['setconfig']['oss_enable']);
$setting['oss_id'] = trim($_POST['setconfig']['oss_id']);
$setting['oss_secret'] = $_POST['setconfig']['oss_secret'];
$setting['oss_bucket'] = trim($_POST['setconfig']['oss_bucket']);
$setting['oss_path'] = trim($_POST['setconfig']['oss_path']);
$setting['oss_upload_url'] = trim($_POST['setconfig']['oss_upload_url']);
打开phpcms\modules\attachment\attachments.php
搜索
$this->groupid = param::get_cookie('_groupid') ? param::get_cookie('_groupid') : 8;
在下方加入
$this->oss = getcache('common','commons'); //载入云存储配置的缓存
再搜索
echo $aids[0].','.$this->upload_url.$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename;
替换为
if($this->oss['oss_enable']){ //图片云存储
echo $aids[0].','.$this->oss['oss_upload_url'].$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename;
}else{
echo $aids[0].','.$this->upload_url.$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename;
}
打开phpcms\libs\classes\attachment.class.php
搜索
var $site = array();
在下方加入
var $oss; //云存储
再搜索
$this->upload_dir = $upload_dir;
在下方加入
$this->oss = getcache('common','commons'); //载入云存储配置的缓存
再搜索
if($watermark_enable) {
$image->watermark($savefile, $savefile);
}
在下方加入
if($this->oss['oss_enable']){ //图片云存储
pc_base::load_app_class('sdk', '' ,0); //载入OSS类
$oss_sdk_service = new ALIOSS();
$oss_sdk_service->set_debug_mode(FALSE); //设置是否打开curl调试模式
$oss_sdk_service->upload_file_by_file($this->oss['oss_bucket'],$this->oss['oss_path'].$filepath,$this->upload_root.$filepath);
}
再搜索
if($thumbs) foreach($thumbs as $thumb) @unlink($thumb);
在下方加入
//删除OSS上的图
if($this->oss['oss_enable']){ //图片云存储
pc_base::load_app_class('sdk', 'attachment' ,0); //载入OSS类
$oss_sdk_service = new ALIOSS();
$oss_sdk_service->set_debug_mode(FALSE); //设置是否打开curl调试模式
$oss_sdk_service->delete_object($this->oss['oss_bucket'],$this->oss['oss_path'].$r['filepath']);
}
5、后台设置(设置 > 相关设置 > 基本设置),对照以下图片
phpcms和uploadfile这个两个都需要手工创建
OSS上的设置
6、最后一点就是将相关模型字段中的editor修改为不保存远程图片:
更新缓存!
本人转自:http://bbs.phpcms.cn/thread-758240-1-1.html
技术支持:小孟网络工作室