【声明】关于六久阁私自出售我公司dedecms小程序插件声明
发布时间:2022-08-25 16:34:58查看:0phpcms对接阿里云OSS 
网上很多phpcms对接阿里云OSS的教程,但是都不全面,所以我在之前别人的基础上做了开发,
使用教程
1、获取阿里云OSS的Access Key、Access Secret ,Bucket名称,节点名称,访问地址等信息,
 

3、打开你的网站找到phpcms/libs/classes/创建文件夹OSS,然后把刚才下载的包解压 找到里边的src文件夹和autoload.php全部复制放进去

4、然后再phpcms/libs/classes/里边创建文件oss.class.php,部分代码:


5、给后台站点的基本设置里边添加几个参数
(1)打开phpcms\modules\admin\templates\setting.tpl.php 搜索
  1. <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> 
后边新增代码:
  1. <tr> 
  2.     <th width="120"><?php echo L('setting_oss_enable')?></th> 
  3.     <td class="y-bg"
  4.     <input name="setconfig[oss_enable]" value="1"  type="radio"  <?php echo ($oss_enable=='1') ? ' checked' : ''?>> <?php echo L('setting_yes')?>     
  5.   <input name="setconfig[oss_enable]" value="0" type="radio"  <?php echo ($oss_enable=='0') ? ' checked' : ''?>> <?php echo L('setting_no')?></td> 
  6.   </tr>  
  7.   <tr> 
  8.     <th width="120"><?php echo L('setting_oss_id')?></th> 
  9.     <td class="y-bg"
  10.       <input type="text" class="input-text" name="setconfig[oss_id]" id="oss_id" size="50" autocomplete="off" value="<?php echo $oss_id?>"/> 
  11.     </td> 
  12.   </tr>        
  13.   <tr> 
  14.     <th width="120"><?php echo L('setting_oss_secret')?></th> 
  15.     <td class="y-bg"><input type="text" class="input-text" name="setconfig[oss_secret]" autocomplete="off"  id="oss_secret" size="50" value="<?php echo $oss_secret?>" /></td> 
  16.   </tr> 
  17.   <tr> 
  18.     <th width="120"><?php echo L('setting_oss_bucket')?></th> 
  19.     <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> 
  20.   </tr>      
  21.   <tr> 
  22.     <th width="120"><?php echo L('setting_oss_path')?></th> 
  23.     <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> 
  24.   </tr>    
  25.   <tr> 
  26.     <th width="120"><?php echo L('setting_oss_upload_url')?></th> 
  27.     <td class="y-bg"><input type="text" class="input-text" name="setconfig[oss_endpoint]" id="oss_endpoint" size="50" value="<?php echo $oss_endpoint?>" /></td> 
  28.   </tr>     
  29.   <tr> 
  30.     <th width="120"><?php echo L('setting_oss_domain')?></th> 
  31.     <td class="y-bg"><input type="text" class="input-text" name="setconfig[oss_domain]" id="oss_upload_url" size="50" value="<?php echo $oss_domain?>" /></td> 
  32.   </tr> 

(2)打开phpcms\languages\zh-cn\admin.lang.php加入以下:
  1. $LANG['setting_oss_enable'] = '启用阿里云存储'
  2. $LANG['setting_oss_id'] = 'Access Key ID'
  3. $LANG['setting_oss_secret'] = 'Access Key Secret'
  4. $LANG['setting_oss_bucket'] = 'Bucket名称'
  5. $LANG['setting_oss_path'] = 'OSS上的图片目录'
  6. $LANG['setting_oss_upload_url'] = '访问路径'
  7. $LANG['setting_oss_domain'] = '访问域名'
(3)打开phpcms\modules\admin\setting.php,找到代码:
  1. $setting['errorlog_size'] = trim($_POST['setting']['errorlog_size']); 
在后边新增代码:
  1. //阿里云云存储配置 
  2. $setting['oss_enable'] = intval($_POST['setconfig']['oss_enable']); 
  3. $setting['oss_id'] = trim($_POST['setconfig']['oss_id']); 
  4. $setting['oss_secret'] = $_POST['setconfig']['oss_secret']; 
  5. $setting['oss_bucket'] = trim($_POST['setconfig']['oss_bucket']); 
  6. $setting['oss_path'] = trim($_POST['setconfig']['oss_path']); 
  7. $setting['oss_endpoint'] = trim($_POST['setconfig']['oss_endpoint']); 
  8. $setting['oss_domain'] = trim($_POST['setconfig']['oss_domain']); 
这样就可以直接在后台添加阿里云OSS的配置参数了

6、打开phpcms\modules\attachment\attachments.php找到代码:
  1. $this->groupid = param::get_cookie('_groupid') ? param::get_cookie('_groupid') : 8; 
后边新增代码:
  1. $this->oss = getcache('common','commons');        //载入云存储配置的缓存 
下边有两种情况:
(1)如果你还是官方原始的图片上传代码,那么按照这个修改
搜索代码:
  1. echo $aids[0].','.$this->upload_url.$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename; 
改成:
  1. if($this->oss['oss_enable']){        //图片云存储 
  2.                         echo $aids[0].','.$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename; 
  3.                     }else
  4.                          echo $aids[0].','.$this->upload_url.$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename; 
  5.                     } 
(2)如果是使用了我都改造版的H5上传,修改方式如下:
搜索代码:
  1. if($attachment->uploadedfiles[0]['isimage']) { 
整个if里边的代码替换成:
  1. if($attachment->uploadedfiles[0]['isimage']) { 
  2.                     if($this->oss['oss_enable']){        //图片云存储 
  3.                         $res = array( 
  4.                             'status' => 1, 
  5.                             'aid' =>  $aids[0], 
  6.                             'filepath' => $attachment->uploadedfiles[0]['filepath'], 
  7.                             'fileext' => $attachment->uploadedfiles[0]['fileext'], 
  8.                             'isimage' => $attachment->uploadedfiles[0]['isimage'], 
  9.                             'title'   => str_replace('.'.$attachment->uploadedfiles[0]['fileext'],'',$filename), 
  10.                             'fileimg' => $attachment->uploadedfiles[0]['filepath'], 
  11.                             'filename' => $filename 
  12.                         ); 
  13.                     }else
  14.                         $res = array( 
  15.                             'status' => 1, 
  16.                             'aid' =>  $aids[0], 
  17.                             'filepath' => $this->upload_url.$attachment->uploadedfiles[0]['filepath'], 
  18.                             'fileext' => $attachment->uploadedfiles[0]['fileext'], 
  19.                             'isimage' => $attachment->uploadedfiles[0]['isimage'], 
  20.                             'title'   => str_replace('.'.$attachment->uploadedfiles[0]['fileext'],'',$filename), 
  21.                             'fileimg' => $this->upload_url.$attachment->uploadedfiles[0]['filepath'], 
  22.                             'filename' => $filename 
  23.                         ); 
  24.                     } 
  25.                     echo json_encode($res); 
  26.                 } else { 
  27.                     $fileext = $attachment->uploadedfiles[0]['fileext']; 
  28.                     if($fileext == 'zip' || $fileext == 'rar') $fileext = 'rar'
  29.                     elseif($fileext == 'doc' || $fileext == 'docx') $fileext = 'doc'
  30.                     elseif($fileext == 'xls' || $fileext == 'xlsx') $fileext = 'xls'
  31.                     elseif($fileext == 'ppt' || $fileext == 'pptx') $fileext = 'ppt'
  32.                     elseif ($fileext == 'flv' || $fileext == 'swf' || $fileext == 'rm' || $fileext == 'rmvb') $fileext = 'flv'
  33.                     else $fileext = 'do'
  34.                     if($this->oss['oss_enable']){        //图片云存储 
  35.                         $res = array( 
  36.                             'status' => 1, 
  37.                             'aid' =>  $aids[0], 
  38.                             'filepath' => $attachment->uploadedfiles[0]['filepath'], 
  39.                             'fileext' => $fileext, 
  40.                             'isimage' => $attachment->uploadedfiles[0]['isimage'], 
  41.                             'title'   => str_replace('.'.$attachment->uploadedfiles[0]['fileext'],'',$filename), 
  42.                             'fileimg' => file_icon($attachment->uploadedfiles[0]['filepath']), 
  43.                             'filename' => $filename 
  44.                         ); 
  45.                     }else
  46.                         $res = array( 
  47.                             'status' => 1, 
  48.                             'aid' =>  $aids[0], 
  49.                             'filepath' => $this->upload_url.$attachment->uploadedfiles[0]['filepath'], 
  50.                             'fileext' => $fileext, 
  51.                             'isimage' => $attachment->uploadedfiles[0]['isimage'], 
  52.                             'title'   => str_replace('.'.$attachment->uploadedfiles[0]['fileext'],'',$filename), 
  53.                             'fileimg' => file_icon($this->upload_url.$attachment->uploadedfiles[0]['filepath']), 
  54.                             'filename' => $filename 
  55.                         ); 
  56.                     } 
  57.                     echo json_encode($res); 
  58.                 }    

(3)如果用的是其他的第三方修改的上传,那么就只能自己改造了

7、phpcms\libs\classes\attachment.class.php找到代码:
  1. var $site = array(); 
后边增加代码:
  1. var $oss;        //云存储 
搜索代码:
  1. $this->upload_dir = $upload_dir; 
在下边加入:
  1. $this->oss = getcache('common','commons');        //载入云存储配置的缓存 
在搜索代码:
  1. if($watermark_enable) { 
  2.     $image->watermark($savefile, $savefile); 
添加代码:(有两处)
  1. if($this->oss['oss_enable']){        //图片云存储 
  2.                     pc_base::load_sys_class('oss''' ,0); 
  3.                     $OSS = new oss(); 
  4.                     $ossData = $OSS->ossUpload($filepath,$this->upload_root.$filepath); 
  5.                     $uploadedfile['filepath'] = $ossData['oss-request-url']; 
  6.                 } 
再搜索代码
  1. function delete($where) 
整个delete方法替换成如下:
  1. function delete($where) { 
  2.         pc_base::load_sys_class('oss''' ,0); 
  3.         $this->oss = getcache('common','commons');  
  4.         $OSS = new oss(); 
  5.         $this->att_db = pc_base::load_model('attachment_model'); 
  6.         $result = $this->att_db->select($where); 
  7.         foreach($result as $r) { 
  8.             $image = str_replace($this->oss['oss_domain'].$this->oss['oss_path'].'/','',$r['filepath']);//替换成相对路径 
  9.             if(strpos($r['filepath'],'http') !== false){ 
  10.                 $OSS->ossDelete($r['filepath']);//oss删除 
  11.             } 
  12.             $image = $this->upload_root.$image; 
  13.             @unlink($image); 
  14.  
  15.             $thumbs = glob(dirname($image).'/*'.basename($image)); 
  16.             if($thumbs) foreach($thumbs as $thumb){ 
  17.                 @unlink($thumb); 
  18.                 $thumb = str_replace($upload_path, '', $thumb); 
  19.                 $OSS->ossDelete($thumb);//oss删除 
  20.             } 
  21.         } 
  22.         return $this->att_db->delete($where); 
  23.     } 
搜到代码:
  1. foreach($remotefileurls as $k=>$file) { 
下边添加代码:
  1. if(strpos($file,$this->oss['oss_domain'])  !== falsecontinue
这一步非常常用,可以防止你的远程OSS图片,被重新下载变成本地图片

8、找到phpcms\modules\attachment\templates\attachment_list.tpl.php找到代码:
  1. <td  align="center"><a href="javascript:preview(<?php echo $info['aid']?>, '<?php echo $info['filename']?>','<?php echo $this->upload_url.$info['filepath']?>')"><?php echo L('preview')?></a> | <a href="javascript:;" onclick="att_delete(this,'<?php echo $info['aid']?>')"><?php echo L('delete')?></a></td> 
把下边的整个td替换成如下:
  1. <td  align="center"><a href="javascript:preview(<?php echo $info['aid']?>, '<?php echo $info['filename']?>','<?php if(strpos($info['filepath'],'http:')!==false) { echo $info['filepath']; }else{  echo $this->upload_url.$info['filepath'];}?>')"><?php echo L('preview')?></a> | <a href="javascript:;" onclick="att_delete(this,'<?php echo $info['aid']?>')"><?php echo L('delete')?></a></td> 
保存一下,更新缓存,即可正常使用

点击付费下载oss类文件,可以协助调试

该篇属于收费内容,请付费查看
注意:阅读码有效期一个月,请及时查看文章内容。

版权声明:本文为原创文章,未经允许不得转载。https://doc.tengcee.com/other/243.html

服务热线

15137100750

我知道你不会打


但是我还是要写


你懂得!

微信二维码

QQ群二维码