qcloudCosChajian.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * 腾讯云上文件存储上传管理
  4. */
  5. include_once(ROOT_PATH.'/include/cos-php-sdk-v4-master/include.php');
  6. use Qcloud\Cos\Api;
  7. class qcloudCosChajian extends Chajian{
  8. private $bucket = 'xinhuoa';
  9. private $folder = 'defile'; //默认目录
  10. private $region = 'sh'; //默认分区
  11. protected function initChajian()
  12. {
  13. $this->bucket = getconfig('qcloudCos_bucket', $this->bucket);
  14. $this->region = getconfig('qcloudCos_region', $this->region);
  15. $this->folder = getconfig('qcloudCos_folder', $this->folder);
  16. $this->app_id = getconfig('qcloudCos_APPID');
  17. $config = array(
  18. 'app_id' => $this->app_id,
  19. 'secret_id' => getconfig('qcloudCos_SecretId'),
  20. 'secret_key'=> getconfig('qcloudCos_SecretKey'),
  21. 'region' => $this->region,
  22. 'timeout' => 180
  23. );
  24. $this->cosApi = new Api($config);
  25. }
  26. /**
  27. * 上传文件
  28. * filepath 要上传的文件全路径
  29. * updir 上传到哪个目录
  30. * upname 上传后保存文件名
  31. */
  32. public function upload($filepath, $updir='', $upname='')
  33. {
  34. if(!file_exists($filepath))return false;
  35. $filea = explode('/', $filepath);
  36. if($upname=='')$upname = $filea[count($filea)-1];
  37. if($updir=='')$updir = $this->folder;
  38. $ret = $this->cosApi->upload($this->bucket, $filepath, ''.$updir.'/'.$upname.'');
  39. if($ret['code']==0)$ret['url'] = $this->geturl().'/'.$updir.'/'.$upname.'';
  40. return $ret;
  41. }
  42. /**
  43. * 创建文件夹
  44. */
  45. public function createFolder($folder)
  46. {
  47. $ret = Cosapi::createFolder($this->bucket, $folder);
  48. return $ret;
  49. }
  50. /**
  51. * 获取目录下的文件
  52. */
  53. public function listFolder($folder='', $num=20)
  54. {
  55. if($folder=='')$folder = $this->folder;
  56. $ret = $this->cosApi->listFolder($this->bucket, $folder, $num);
  57. if($ret['code'] != 0){
  58. return returnerror($ret['message']);
  59. }else{
  60. $barr = returnsuccess($ret['data']['infos']);
  61. $barr['folder'] = $folder;
  62. return $barr;
  63. }
  64. }
  65. /**
  66. * 删除文件
  67. */
  68. public function delFile($path)
  69. {
  70. $ret = $this->cosApi->delFile($this->bucket, $path);
  71. return $ret;
  72. }
  73. public function delListFile()
  74. {
  75. $barr = $this->listFolder('',100);
  76. if($barr['success']){
  77. foreach($barr['data'] as $k=>$rs){
  78. $this->delFile($this->folder.'/'.$rs['name']);
  79. }
  80. }
  81. }
  82. /**
  83. * 下载文件到服务器本地
  84. */
  85. public function download($srcPath, $dstPath)
  86. {
  87. $res = $this->cosApi->download($this->bucket, $srcPath, $dstPath);
  88. return $res;
  89. }
  90. /**
  91. * 获取外网访问地址
  92. */
  93. public function geturl()
  94. {
  95. $xarr['nj'] = 'ap-nanjing';
  96. $xarr['cd'] = 'ap-chengdu';
  97. $xarr['bj'] = 'ap-beijing';
  98. $xarr['gz'] = 'ap-guangzhou';
  99. $xarr['sh'] = 'ap-shanghai';
  100. $xarr['cq'] = 'ap-chongqing';
  101. $xarr['bjfsi'] = 'ap-beijing-fsi';
  102. $xarr['szfsi'] = 'ap-shenzhen-fsi';
  103. $xarr['shfsi'] = 'ap-shanghai-fsi';
  104. $xarr['hk'] = 'ap-hongkong';
  105. $qustr = arrvalue($xarr, $this->region,'ap-shanghai');
  106. $url = 'https://'.$this->bucket.'-'.$this->app_id.'.cos.'.$qustr.'.myqcloud.com';
  107. return $url;
  108. }
  109. }
粤ICP备19079148号