thinkphp 七牛操作

thinkphp框架给我提供了七牛云文件上传
但是仅仅只有文件上传,因为其他原因,我需要将原来的图片删除,节省空间
当我导入了七牛提供的SDK,在thinkphp中,我又不知道如何操作,总是出现错误
于是就自己看七牛官方提供的文档,写了个文件删除的出来

利用thinkphp提供的上传文件到七牛:

[php]
$config = array (
‘maxSize’ => 5 * 1024 * 1024,
‘rootPath’ => ‘.’,
‘savePath’ => ”,
‘saveName’ => array (
‘uniqid’,

),
‘exts’ => array (
‘jpg’,
‘gif’,
‘png’,
‘jpeg’
),
‘autoSub’ => true,
‘subName’ => array (
‘date’,
‘Ymd’
)
);
$config_qiniu = array (
‘secretKey’ => readConfig("QINIU_SK"),//七牛的SK https://portal.qiniu.com/user/key
‘accessKey’ => readConfig("QINIU_AK"),//七牛的AK
‘domain’ => readConfig("QINIU_DOMAIN"),//与七牛绑定的域名
‘bucket’ => readConfig("QINIU_BUCKET")//空间名字
);
$upload = new \Think\Upload ( $config, ‘Qiniu’, $config_qiniu );
$info = $upload->upload ( $_FILES );
if (! $info) { // 上传错误提示错误信息
//错误
} else {
//成功
}
[/php]

然后我自己写的删除文件:

[php]
function deletefile_Qiniu($filename) {
$encodedEntryURI = base64_urlSafeEncode ( readConfig("QINIU_BUCKET").":". $filename );
$Str = "/delete/$encodedEntryURI\n";
$encode = hash_hmac ( ‘sha1’, $Str, readConfig("QINIU_SK"), true );
$encode = base64_urlSafeEncode ( $encode );
$accessToken = readConfig("QINIU_AK").":$encode";
$headers = array (
‘Authorization: QBox ‘ . $accessToken,
‘Content-Type: text/html’
);
$url = ‘http://rs.qiniu.com/delete/’ . $encodedEntryURI;
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
function base64_urlSafeEncode($data) {
$find = array (
‘+’,
‘/’
);
$replace = array (
‘-‘,
‘_’
);
return str_replace ( $find, $replace, base64_encode ( $data ) );
}
[/php]

如果还需要其他的,比如获取文件信息等等
只需要将
$Str = “/delete/$encodedEntryURI\n”;
$url = ‘http://rs.qiniu.com/delete/’ . $encodedEntryURI;
中的delete改成相应的命令即可
具体还请参考这里:http://developer.qiniu.com/article/index.html#kodo-api-handbook

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注