发布阿里云OSS for phpcmsV9整合教程

说明:这个算不上是插件,因为没有安装包,需要手工修改代码。
还有一点就是后台发布文章时上传的附件还是会保存在你的服务器上,基于以下原因:
1、个人的需求是前台页面需要使用thumb函数生成多个缩略图大小,thumb函数是不支持远程图片的(保存在OSS上就变远程图片了),即使修改为支持远程图片,也需要将远程图片先保存到本地,这样速度会非常慢,是非常非常;
2、做备份的需要,这点你懂得,某天OSS上的附件没有了就悲剧了,所以使用这个OSS for 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>
  </tr>

在下方添加

<!--阿里云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')?>&nbsp;&nbsp;&nbsp;&nbsp;
  <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上的设置


5、最后一点就是将相关模型字段中的editor修改为不保存远程图片:

傲游截图20130125153107.jpg (43.81 KB, 下载次数: 0)

下载附件  保存到相册

2013-1-25 15:31 上传



更新缓存!


看下图的路径

转载:bs.phpcms.cn/thread-758240-1-1.html

posted on 2015-12-29 02:35  今天又进步了  阅读(1368)  评论(0编辑  收藏  举报

导航