您的位置:首页 > 其它

七牛云单页面上传图片的功能

2014-03-20 15:17 351 查看
<?php
date_default_timezone_set("Asia/Shanghai"); //设置时区

//插入七牛云的类库
require_once('../../library/qiniu/rs.php');
require_once('../../library/qiniu/http.php');
require_once('../../library/qiniu/auth_digest.php');
require_once('../../library/qiniu/utils.php');

$bucket = "qn";
$domain = 'qn.qiniudn.com';
$accessKey = 'ak';
$secretKey = 'sk';

Qiniu_SetKeys($accessKey, $secretKey);
$client = new Qiniu_MacHttpClient(null);    //查询是否已存在链接

//$deadline = time() + 315360000;
$deadline = 1735660800;	//2025-01-01 00:00:00
/***** 10 年有效 *****/
$keyEsc = rawurlencode($_GET['filename']);
$baseUrl = "http://$domain/$keyEsc";

$pos = strpos($baseUrl, '?');
if ($pos !== false) {
$baseUrl .= '&e=';
} else {
$baseUrl .= '?e=';
}
$baseUrl .= $deadline;
$sign = hash_hmac('sha1', $baseUrl, $secretKey, true);
$token = $accessKey . ':' . Qiniu_Encode($sign);
$thisurl = "$baseUrl&token=$token";
//echo $thisurl;

$putPolicy = new Qiniu_RS_PutPolicy($bucket);
//$putPolicy->ReturnUrl = $thisurl;
$upToken = $putPolicy->Token(null);
//echo $upToken;

?>
<html>
<head>
<script type="text/javascript" src="../../static/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function(){
$('.upload-pic-btn').on('click', function() {
$('#image-input').click();
});

$('#image-input').change(function(event) {
var file = this.files[0];
if (file == null) {
return;
}else{

<?php
//如果已经存在图片,则删除网盘中的图片再上传新的图片。
//list($ret, $err) = Qiniu_RS_Stat($client, $bucket, $_GET['filename']); //判断是否已存在img
Qiniu_RS_Delete($client, $bucket, $_GET['filename']);
?>
}
$("label").remove();

var formData = new FormData();
formData.append("file", file);
formData.append("token", "<?php echo $upToken; ?>");
formData.append("key", "<?php echo $_GET['filename']; ?>");

$.ajax({
url: 'http://up.qiniu.com/',  //Server script to process data
type: 'POST',
success: function (data) {
//var imgurl = data["x:imgurl"];
//$("button").after("<br /><label><?php echo $thisurl; ?></label>");
window.location.href="admin.php?action=upload&ctrl=exeqn&inid=<?php echo $_GET['inid']; ?>&url=<?php echo urlencode($thisurl); ?>";
},
error: function () {
//location.reload();
$("button").after("<br /><label><b>上传失败</b></label>,<a href='<?php echo $_SERVER["REQUEST_URI"]; ?>'>重新载入页面上传。</a>");
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
});
</script>
</head>
<body>
<button name="imageFile" class="upload-pic-btn">上传图片</button>
<input type="file" id="image-input" style="display:none">
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: