實例 | tp5使用七牛雲上傳圖片和文件/刪除文件


thinkphp5中使用七牛雲

下載 https://github.com/qiniu/php-sdk
vendor文件下

1 新建文件 application/extra/qiniu.php
2 return [
3 'ACCESSKEY' => 'PkQcItX7rJL7',//你的accessKey
4 'SECRETKEY' => 'eaWM7C_COYLjX8VKVGinzv1',//你的secretKey
5 'BUCKET' => 'zin',//上傳的空間
6 'DOMAIN'=>'pdn.com'//空間綁定的域名
7 ];

html

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <form action="{:url('test/upload')}" method="post"enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="image" id="image" /> 
    <br />
    <input type="submit" name="submit" value="Submit" />
  </form>
</body>
</html>

php

 1 <?php
 2 namespace app\index\controller;
 3 use think\Config;
 4 use think\Image;
 5 use think\Request;
 6 use Qiniu\Auth as Auth;
 7 use Qiniu\Storage\BucketManager;
 8 use Qiniu\Storage\UploadManager;
 9  
10 class Test
11 {
12   // 上傳
13     public function test()
14     {
15         if(request()->isPost()){
16             $file = request()->file('image');
17             // 要上傳圖片的本地路徑
18             $filePath = $file->getRealPath();
19             $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后綴
20  
21             // 上傳到七牛后保存的文件名
22             $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
23             require_once APP_PATH . '/../vendor/qiniu/autoload.php';
24             // 需要填寫你的 Access Key 和 Secret Key
25             $accessKey = config("qiniu.ACCESSKEY");
26             $secretKey = config("qiniu.SECRETKEY");
27             // 構建鑒權對象
28             $auth = new Auth($accessKey, $secretKey);
29             // 要上傳的空間
30             $bucket = config("qiniu.BUCKET");
31             $domain = config("qiniu.DOMAINImage");
32             $token = $auth->uploadToken($bucket);
33             // 初始化 UploadManager 對象並進行文件的上傳
34             $uploadMgr = new UploadManager();
35             // 調用 UploadManager 的 putFile 方法進行文件的上傳
36             list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
37             if ($err !== null) {
38                 return json(["err"=>1,"msg"=>$err,"data"=>""]);die;
39             } else {
40                 //返回圖片的完整URL
41                 return  json(["err"=>0,"msg"=>"上傳完成","data"=>uploadreg($domain . $ret['key'])]);die;
42             }
43         }
44         return view(); 
45     }
46  
47     //  刪除
48     public function delete($name)
49     {
50         $delFileName = input("name");
51         if( $delFileName ==null){
52             echo "參數不正確";die;
53         }
54         require_once APP_PATH . '/../vendor/qiniu/autoload.php';
55         // 構建鑒權對象
56         $auth = new Auth(config("qiniu.ACCESSKEY"),config("qiniu.SECRETKEY"));
57  
58         // 配置
59         $config = new \Qiniu\Config();
60  
61         // 管理資源
62         $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
63  
64         // 刪除文件操作
65         $res = $bucketManager->delete(config("qiniu.BUCKET"), $delFileName);
66  
67         if (is_null($res)) {
68             // 為null成功
69             // return true;
70             echo "成功";
71         }else{
72             echo "失敗";
73         }
74     }
75     
76     
77 }
78  
79 ?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM