七牛上传整合CI

1. 下载七牛官方php-sdk,将src下的Qiniu放入libraries

2. 新建libraries/Qiniu/Autoloader.php

<?php
namespace Qiniu;
 
class Autoloader {
    private $directory;
    private $prefix;
    private $prefixLength;
 
    public function __construct($baseDirectory = __DIR__)
    {
        $this->directory = $baseDirectory;
        $this->prefix = __NAMESPACE__.'\\';
        $this->prefixLength = strlen($this->prefix);
    }
 
    public function autoload($class)
    {
        if (0 === strpos($class, $this->prefix)) {
            $parts = explode('\\', substr($class, $this->prefixLength));
            $filepath = $this->directory . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $parts) . '.php';
 
            if (is_file($filepath)) {
                require $filepath;
            }
        }
    }
 
    public static function register()
    {
        spl_autoload_register(array(new self(), 'autoload'));
    }
}

3. 新建libraries/Qiniuload.php

<?php

require 'Qiniu/Autoloader.php';

class QiniuLoad
{
    public function __construct()
    {
        Qiniu\Autoloader::register();
        require 'Qiniu/functions.php';
    }
}

4. 新建控制器controllers/qiniu/Qiniu.php

<?php

class Qiniu extends CI_Controller
{
    public $accessKey;
    public $secretKey;
    public $domain;
    public $bucket;

    function __construct($accessKey, $secretKey, $domain, $bucket)
    {
        parent::__construct();
        $this->load->library('Qiniuload');
        $this->accessKey = $accessKey;
        $this->secretKey = $secretKey;
        $this->domain = $domain;
        $this->bucket = $bucket;
    }

    // 上传文件
    public function uploadFile($filePath, $key = null, $bucket = '')
    {
        if (!file_exists($filePath)) {
            throw new \Exception(400, '上传的文件不存在');
        }
        // 构建鉴权对象
        $auth = new Qiniu\Auth($this->accessKey, $this->secretKey);
        // 生成上传Token
        $bucket = $bucket ? $bucket : $this->bucket;
        $token = $auth->uploadToken($bucket);
        // 初始化上传对象
        $uploadMgr = new Qiniu\Storage\UploadManager();
        // 进行上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            var_dump($err);
        } else {
            return true;
        }
    }
    // 删除文件
    public function delete($key, $bucket = '')
    {
        $bucket = $bucket ? $bucket : $this->bucket;
        $auth = new Qiniu\Auth($this->accessKey, $this->secretKey);
        $config = new \Qiniu\Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
        $err = $bucketManager->delete($bucket, $key);
        if ($err) {
            print_r($err);
        }
    }
    // 获取资源链接
    public function getLink($key = '')
    {
        $url = \rtrim($this->domain, '/') . "/{$key}";
        return $url;
    }
}

5. 使用案例controllers/uploadvideo/uploadvideo.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

include_once dirname(dirname(__FILE__))."/aes/wxBizDataCrypt.php";
include_once dirname(dirname(__FILE__))."/qiniu/Qiniu.php";

class Uploadvideo extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('uploadvideo/Uploadvideo_model');
        $this->load->model('UserInfo_model');
    }
// 删除视频
    public function del($videoid)
    {
        $video = $this->db->get_where('upload_video', ['videoid' => $videoid])->row_array();
        $this->Uploadvideo_model->del($videoid);
        $qiniu = new Qiniu(AK, SK, DOMAIN, BUCKET);
        $qiniu->delete(basename($video['src']));
        $qiniu->delete(basename($video['labelimg']));
        echo 'true';
    }
// 上传存储文件
    public function upload()
    {
        if ($_FILES['file']['error'] > 0) {
            echo '上传错误,您文件太大';
        } else {
            // $save_dir = 'upload'.DIRECTORY_SEPARATOR.'uploadvideo';
            // if (!file_exists($save_dir)) {
            //     mkdir($save_dir, 0777, true);
            // }
            // $file_name_array = explode('.', $_FILES['file']['name']);
            // $save_path = $save_dir.DIRECTORY_SEPARATOR.md5(uniqid()).'.'.end($file_name_array);
            // move_uploaded_file($_FILES['file']['tmp_name'], $save_path);
            // echo str_replace('\\', '/', base_url().$save_path);

            $qiniu = new Qiniu(AK, SK, DOMAIN, BUCKET);
            $key = uniqid();
            $qiniu->uploadFile($_FILES['file']['tmp_name'], $key);
            echo 'http://' . $qiniu->getLink($key);
        }
    }
}

 

posted @ 2018-08-15 17:12  maoriaty  阅读(350)  评论(0编辑  收藏  举报