如何在thinkphp5框架中图片上传

发布时间:2019-09-12 21:14:43

七牛云存储的好处我就不多说了,如何在thinkphp5框架中,正确配置和使用七牛sdk,成功的将文件或图片上传至七牛空间中,并获得图片的信息!

下面也是将我在网站建设中,摸索得到的成功代码分析给大家:

前提就不多说了,起码在七牛官网上得到自己的AK,SK,bucket等信息

第一步:配置

    'domain' => 'https://www.example.com',   //你的七牛域名,支持 http 和 https,也可以不带协议,默认 http

            'access_key' => '',                       //AccessKey

            'secret_key' => '',                       //SecretKey

            'bucket'     => '',                      //Bucket名字

然后写php文件,引入七牛的sdk

<?php
  namespace app\index\controller;
  use think\Config;
  use think\Image;
  use think\Request;
  use Qiniu\Auth as Auth;
  use Qiniu\Storage\BucketManager;
  use Qiniu\Storage\UploadManager;
  
class Test
 {
   // 上传
     public function test()
     {
         if(request()->isPost()){
             $file = request()->file('image');
             // 要上传图片的本地路径
             $filePath = $file->getRealPath();
             $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
  
             // 上传到七牛后保存的文件名
             $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
             require_once APP_PATH . '/../vendor/qiniu/autoload.php';
             // 需要填写你的 Access Key 和 Secret Key
             $accessKey = config("qiniu.ACCESSKEY");
            $secretKey = config("qiniu.SECRETKEY");
             // 构建鉴权对象
             $auth = new Auth($accessKey, $secretKey);
             // 要上传的空间
             $bucket = config("qiniu.BUCKET");
             $domain = config("qiniu.DOMAINImage");
             $token = $auth->uploadToken($bucket);
             // 初始化 UploadManager 对象并进行文件的上传
             $uploadMgr = new UploadManager();
             // 调用 UploadManager 的 putFile 方法进行文件的上传
             list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
             if ($err !== null) {
                 return json(["err"=>1,"msg"=>$err,"data"=>""]);die;
             } else {
                 //返回图片的完整URL
                 return  json(["err"=>0,"msg"=>"上传完成","data"=>uploadreg($domain . $ret['key'])]);die;
             }
         }
         return view(); 
     }
 
     //  删除
    public function delete($name)
     {
         $delFileName = input("name");

        if( $delFileName ==null){
            echo "参数不正确";die;
         }
         require_once APP_PATH . '/../vendor/qiniu/autoload.php';
         // 构建鉴权对象
         $auth = new Auth(config("qiniu.ACCESSKEY"),config("qiniu.SECRETKEY"));
  
        // 配置
         $config = new \Qiniu\Config();
  
         // 管理资源
         $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
  
         // 删除文件操作
         $res = $bucketManager->delete(config("qiniu.BUCKET"), $delFileName);
  
         if (is_null($res)) {
             // 为null成功
             // return true;
             echo "成功";
         }else{
             echo "失败";
         }
     }
     
     
}
 
?>

我在网站建设中,摸索得到的成功代码分享给大家 希望对大家有帮助

远近互联后端小秦整理发布,希望能对学习技术的你有所帮助
远近互联专业提供网站建设、APP开发、网站优化、外贸网站SEO、微信运营的品牌整合营销服务让客户通过网络品牌建立与网络传播提高业绩。


【相关推荐】