网站通常要把图片存到图片服务器上,以减少本地空间压力。所以可以用七牛免费空间来储存图片。
首先,在七牛上申请一个帐号。建立一个图片空间。
/*七牛配置*/
$qiniu_accessKey = 'your key';
$qiniu_secretKey = 'your key';
$qiniu_bucket = 'your bucket';
$qiniu_flag = 1; //开启七牛图片
$qiniu_addr = "your address";
//上传到七牛, $image = file , $pics = pic_name
function upload_to_Qiniu($image, $pics){
global $qiniu_accessKey, $qiniu_secretKey, $qiniu_bucket, $qiniu_flag;
if($qiniu_flag){
$auth = new Auth($qiniu_accessKey, $qiniu_secretKey);
$token = $auth->uploadToken($qiniu_bucket);
$filePath = $image['tmp_name'];
$key = $pics;
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
return 1;
}
else
return 0;
}
//删除七牛图片
function del_Qiniu($picname){
global $qiniu_accessKey, $qiniu_secretKey, $qiniu_bucket, $qiniu_flag;
if($qiniu_flag){
$auth = new Auth($qiniu_accessKey, $qiniu_secretKey);
$bucketMgr = new BucketManager($auth);
$bucket = $qiniu_bucket;
$key = $picname;
$err = $bucketMgr->delete($bucket, $key);
return 1;
}
else
return 0;
}