七牛雲單頁面上傳圖片的功能
阿新 • • 發佈:2019-01-23
<?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>