废话不多说,直接上菜,希望这道菜能符合大家的口味,^_^
上传图片页:
<?php
include "./autoload.php";use Qiniu\Auth;
use Qiniu\Storage\UploadManager; $bucket = '你的bucket(即你的七牛空间名)';
$accessKey = '你的accessKey(在你的七牛个人空间中的密钥管理即可找到) ';
$secretKey = '你的secretKey (在你的七牛个人空间中的密钥管理即可找到)';
$expires = 6000;$auth = new Auth($accessKey, $secretKey);
$policy = array(
'callbackBody' => 'key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)&name=$(x:name)',
'callbackBodyType' => 'application/json'
);
$token = $auth->uploadToken($bucket, null, $expires, $policy, true);
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
?>
<form method="post" action="up.php" enctype="multipart/form-data">
<input name="token" type="hidden" value="<?php echo $token;?>">
<input name="file" type="file" />
<input type="submit" value="上传"/>
</form>
服务页(up.php)
<?php
include "./autoload.php";
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
$bucket = '你的bucket(即你的七牛空间名)';
$accessKey = '你的accessKey(在你的七牛个人空间中的密钥管理即可找到) ';
$secretKey = '你的secretKey (在你的七牛个人空间中的密钥管理即可找到)';
$expires = 6000;
$auth = new Auth($accessKey, $secretKey);
$policy = array(
'callbackBody' => 'key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)&name=$(x:name)',
'callbackBodyType' => 'application/json'
);
$token = $_POST['token'];// 这样能确保前后token一致,不会报bad token 错误
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
// 要上传文件的本地路径
$filePath = $_FILES['file']['tmp_name'];
// 上传到七牛后保存的文件名
$key = date("YmdHis")."".$_FILES['file']['name'];
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
echo " putFile result: \n";
if ($err !== null) {
echo "error:";
var_dump($err);
} else {
echo "success:";
var_dump($ret);
}
最后为大家附上源码链接: https://pan.baidu.com/s/1xeiWi7VsBEELIZPXjtj6Vw 密码: mryd