博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
七牛 PHP 文件管理
阅读量:2095 次
发布时间:2019-04-29

本文共 5968 字,大约阅读时间需要 19 分钟。

<?php
class QiniuClient
{
const UP_HOST = 'http://up.qiniu.com';
const RS_HOST = 'http://rs.qbox.me';
const RSF_HOST = 'http://rsf.qbox.me';
public $accessKey;
public $secretKey;
function __construct($accessKey='',$secretKey='')
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
}
public function uploadFile($filePath,$bucket,$key=null)
{
$uploadToken = $this->uploadToken(array('scope' => $bucket));
$data = array();
$data['file'] = "@$filePath";
$data['token'] = $uploadToken;
if($key) $data['key'] = $key;
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::UP_HOST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function upload($content,$bucket,$key=null)
{
$filePath = tempnam(sys_get_temp_dir(), 'UPLOAD');
file_put_contents($filePath, $content);
$result = $this->uploadFile($filePath,$bucket,$key);
unlink($filePath);
return $result;
}
public function uploadRemote($url,$bucket,$key=null)
{
$filePath = tempnam(sys_get_temp_dir(), 'UPLOAD');
copy($url,$filePath);
$result = $this->uploadFile($filePath,$bucket,$key);
unlink($filePath);
return $result;
}
public function stat($bucket,$key)
{
$encodedEntryURI = self::urlsafe_base64_encode("{$bucket}:{$key}");
$url = "/stat/{$encodedEntryURI}";
return $this->fileHandle($url);
}
public function move($bucket,$key,$bucket2,$key2=false)
{
if(!$key2) {
$key2 = $bucket2;
$bucket2 = $bucket;
}
$encodedEntryURISrc = self::urlsafe_base64_encode("{$bucket}:{$key}");
$encodedEntryURIDest = self::urlsafe_base64_encode("{$bucket2}:{$key2}");
$url = "/move/{$encodedEntryURISrc}/{$encodedEntryURIDest}";
return $this->fileHandle($url);
}
public function copy($bucket,$key,$bucket2,$key2=false)
{
if(!$key2) {
$key2 = $bucket2;
$bucket2 = $bucket;
}
$encodedEntryURISrc = self::urlsafe_base64_encode("{$bucket}:{$key}");
$encodedEntryURIDest = self::urlsafe_base64_encode("{$bucket2}:{$key2}");
$url = "/copy/{$encodedEntryURISrc}/{$encodedEntryURIDest}";
return $this->fileHandle($url);
}
public function delete($bucket,$key)
{
$encodedEntryURI = self::urlsafe_base64_encode("{$bucket}:{$key}");
$url = "/delete/{$encodedEntryURI}";
return $this->fileHandle($url);
}
// $operator = stat|move|copy|delete 
// $client->batch('stat',array('square:test/test5.txt','square:test/test13.png'));
public function batch($operator,$files)
{
$data = '';
foreach ($files as $file) {
if(!is_array($file)) {
$encodedEntryURI = self::urlsafe_base64_encode($file);
$data.="op=/{$operator}/{$encodedEntryURI}&";
}else{
$encodedEntryURI = self::urlsafe_base64_encode($file[0]);
$encodedEntryURIDest = self::urlsafe_base64_encode($file[1]);
$data.="op=/{$operator}/{$encodedEntryURI}/{$encodedEntryURIDest}&";
}
}
return $this->fileHandle('/batch',$data);
}
public function listFiles($bucket,$limit='',$prefix='',$marker='')
{
$params = array_filter(compact('bucket','limit','prefix','marker'));
$url = self::RSF_HOST.'/list?'.http_build_query($params);
return $this->fileHandle($url);
}
public function fileHandle($url,$data=array())
{
if(strpos($url, 'http://')!==0) $url = self::RS_HOST.$url;
if(is_array($data)) $accessToken = $this->accessToken($url);
else $accessToken = $this->accessToken($url,$data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Authorization: QBox '.$accessToken,
   ));
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   // If $data is an array, the Content-Type header will be set to multipart/form-data
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $result = curl_exec($ch);
   $info = curl_getinfo($ch);
curl_close($ch);
if($info['http_code']>=300)
throw new Exception($info['http_code'].': '.$result);
if($info['content_type']=='application/json')
return json_decode($result,true);
return $result;
}
public function uploadToken($flags)
{
if(!isset($flags['deadline']))
$flags['deadline'] = 3600 + time();
$encodedFlags = self::urlsafe_base64_encode(json_encode($flags));
$sign = hash_hmac('sha1', $encodedFlags, $this->secretKey, true);
$encodedSign = self::urlsafe_base64_encode($sign);
   $token = $this->accessKey.':'.$encodedSign. ':' . $encodedFlags;
   return $token;
}
public function accessToken($url,$body=false){
   $parsed_url = parse_url($url);
   $path = $parsed_url['path'];
   $access = $path;
   if (isset($parsed_url['query'])) {
       $access .= "?" . $parsed_url['query'];
   }
   $access .= "\n";
   if($body) $access .= $body;
   $digest = hash_hmac('sha1', $access, $this->secretKey, true);
   return $this->accessKey.':'.self::urlsafe_base64_encode($digest);
}
public static function urlsafe_base64_encode($str){
   $find = array("+","/");
   $replace = array("-", "_");
   return str_replace($find, $replace, base64_encode($str));
}

}

<?php

require_once("QiniuClient.php");
    
    
$accessKey = '';
$secretKey = '';
$client  = new QiniuClient($accessKey,$secretKey);
$items = $client->listFiles('downloader',$limit=1000,$prefix='',$marker='');
//print_r($items);
function delother()
{
global $items;
global $client;
$size = count($items['items']);
for($i=0;$i<$size;$i++) {
    if(strpos($items['items'][$i]['mimeType'],'pdf')==0) 
  {
    $client->delete("downloader",$items['items'][$i]['key']);
    
  }
  
 }
 
} //end fun
 
 
 
/**********************
一个简单的目录递归函数
第一种实现办法:用dir返回对象
***********************/
function tree($directory) 
$mydir = dir($directory); 
while($file = $mydir->read())
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")) 
{
//echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n"; 
tree("$directory/$file"); 
else 
//echo "<li>$file</li>\n"; 
//echo "</ul>\n"; 
$mydir->close(); 
function uploadpdf($directory)
{
global $client;
$mydir = dir($directory); 
while($file = $mydir->read())
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")) 
{
tree("$directory/$file"); 
else 
{
      echo $file;
}
 
$mydir->close(); 
}
 //delother();
 uploadpdf("D:\\test\");
 
 
?>

转载地址:http://vnuhf.baihongyu.com/

你可能感兴趣的文章
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>
阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
查看>>
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>