• 七牛php上传下载类,集成官方文档的方法


    <?php
    use QiniuAuth;
    use QiniuStorageUploadManager;
    class qiniu
    {
    public $_accesskey = null;
    public $_secretKey =null;
    public $_bucket =null;
    /**
    * 构造函数
    *
    * @access public
    * @param string $tpl
    * @return void
    */
    function __construct($accesskey=null,$secretKey=null,$bucket =null)
    {
    $this->_accesskey=$accesskey;
    $this->_secretKey=$secretKey;
    $this->_bucket=$bucket;
    }
    function gettoken(){

    $auth = new Auth($this->_accessKey, $this->_secretKey);
    $bucket = $this->_bucket;
    $token = $auth->uploadToken($bucket);
    return $token;
    }
    function uploadstring($string){
    //$string是字符串

    $auth = new Auth($this->_accesskey, $this->_secretKey);
    $bucket = $this->_bucket;
    // 设置put policy的其他参数, 上传回调
    //$opts = array(
    // 'callbackUrl' => 'http://www.callback.com/',
    // 'callbackBody' => 'name=$(fname)&hash=$(etag)'
    // );
    //$token = $auth->uploadToken($bucket, null, 3600, $opts);

    $token = $auth->uploadToken($bucket);
    $uploadMgr = new UploadManager();

    list($ret, $err) = $uploadMgr->put($token, null, $string);
    echo " ====> put result: ";
    if ($err !== null) {
    return $err;
    } else {
    return $ret;
    }
    }
    function uploadfile($file=null){
    //$file是文件路径

    $auth = new Auth($this->_accesskey, $this->_secretKey);
    $bucket = $this->_bucket;
    // 设置put policy的其他参数, 上传回调
    //$opts = array(
    // 'callbackUrl' => 'http://www.callback.com/',
    // 'callbackBody' => 'name=$(fname)&hash=$(etag)'
    // );
    //$token = $auth->uploadToken($bucket, null, 3600, $opts);

    $token = $auth->uploadToken($bucket);
    $uploadMgr = new UploadManager();
    list($ret, $err) = $uploadMgr->putFile($token, null, $file);
    if ($err !== null) {
    return $err;
    } else {
    return $ret;
    }
    }
    function download($resource,$filecode,$tosource=null,filename="file"){

    //$resource是bucket的网址,$filecode是文件的码,$tosource是要下载的文件夹路径
    $auth = new Auth($this->_accesskey, $this->_secretKey);
    $baseUrl = $resource.'/'.$filecode;
    $authUrl = $auth->privateDownloadUrl($baseUrl);
    $this->download_remote_file_with_curl($authUrl, $tosource.time().$filename);
    return $authUrl;
    }
    function download_remote_file_with_curl($file_url, $save_to)
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch,CURLOPT_URL,$file_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $file_content = curl_exec($ch);
    curl_close($ch);

    $downloaded_file = fopen($save_to, 'w');
    fwrite($downloaded_file, $file_content);
    fclose($downloaded_file);
    }
    }


    ?>

  • 相关阅读:
    luogu 1865 数论 线性素数筛法
    洛谷 2921 记忆化搜索 tarjan 基环外向树
    洛谷 1052 dp 状态压缩
    洛谷 1156 dp
    洛谷 1063 dp 区间dp
    洛谷 2409 dp 月赛题目
    洛谷1199 简单博弈 贪心
    洛谷1417 烹调方案 dp 贪心
    洛谷1387 二维dp 不是特别简略的题解 智商题
    2016 10 28考试 dp 乱搞 树状数组
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4688665.html
Copyright © 2020-2023  润新知