Home > Article > Backend Development > uploadify implements Qiniu Cloud storage to display upload progress + page display, uploadify Niu Cloud_PHP tutorial
Preparation:
uploadify download address:
http://www.uploadify.com/download/
Qiniu php-sdk development guide:
http://developer.qiniu.com/docs/v6/sdk/php-sdk.html
php-sdk address:
https://github.com/qiniu/php-sdk
Start:
DEMO:
http://hxend.com/uploadif/
After registering an account in Qiniu, you will become a standard user
Free storage space 10GB
Free monthly download traffic 10GB
Free monthly PUT/DELETE 100,000 requests
Free 1 million GET requests per month
Seems like a good benefit.
After successful registration, the account page will have ak and sk keys that can be used in the code.
After downloading uploadify, put the contents of the Qiniu php -sdk file package into uploadify
Open the uploadify.php file and the code is as follows:
<?php /* Uploadify Copyright (c) 2012 Reactive Apps, Ronnie Garcia Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> */ // Define a destination $targetFolder = '/uploads'; // Relative to the root $verifyToken = md5('unique_salt' . $_POST['timestamp']); if (!empty($_FILES) && $_POST['token'] == $verifyToken) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; // Validate the file type $fileTypes = array('jpg','jpeg','gif','png'); // File extensions $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; } } ?>
Modify the code as follows: Introduce the internal reference code.
<?php $verifyToken = md5('unique_salt' . $_POST['timestamp']); if (!empty($_FILES) && $_POST['token'] == $verifyToken) { $tempFile = $_FILES['Filedata']['tmp_name']; //生成新的文件名 $filename = time().mt_rand(10,99).'.'.end(explode('.', $_FILES['Filedata']['name'])); //在这里修改生出随机图片名 $fileTypes = array('jpg','jpeg','gif','png'); //限制上传的文件为图片 $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { //上传图片到云端 start require_once("qiniu/io.php"); require_once("qiniu/rs.php"); $bucket = "hdimg";//空间名 //截取原始文件后缀名 $key1 = "Uploads/".$filename; $accessKey = ' '; //这里填写ak $secretKey = ' '; // 这里填写SK Qiniu_SetKeys($accessKey, $secretKey); $putPolicy = new Qiniu_RS_PutPolicy($bucket); $upToken = $putPolicy->Token(null); $putExtra = new Qiniu_PutExtra(); $putExtra->Crc32 = 1; //$tempFile uploadify上传的临时文件路径 list($ret, $err) = Qiniu_PutFile($upToken, $key1, $tempFile, $putExtra); //上传图片到云端 end //返回文件名给前台 echo "http://hdimg.qiniudn.com/".$key1; //前台使用回调函数的data参数接收 } else { echo 'Invalid file type.'; } }
The front-end index.php is modified to: the front-end calls the value data output by echo to operate.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>UploadiFive Test</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script> <script src="jquery.uploadify.min.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="uploadify.css"> <style type="text/css"> body { font: 13px Arial, Helvetica, Sans-serif; } </style> </head> <body> <form> <div id="queue"></div> <input id="file_upload" name="file_upload" type="file" multiple="true"> </form> <img width="100" height="100" id="txtimg" src=""/> <script type="text/javascript"> <?php $timestamp = time();?> $(function() { $('#file_upload').uploadify({ 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', 'token' : '<?php echo md5('unique_salt' . $timestamp);?>' }, 'swf' : 'uploadify.swf', 'uploader' : 'uploadify.php', 'onUploadSuccess' : function(file,data,response) { //执行成功后就执行该段js document.getElementById('txtimg').src=data; } }); }); </script> </body> </html>
Input data to the page to display the current page. The value of control #txtimg is the output data value, which is the image address.
If you need to call iframe later, you can use
document.getElementById('txtimg').src=data; 可以把data 传输到父页面 的 #txtimg 中。
parent.document.getElementById('txtimg').src=data;
aafd4e4d4d257afd8398be4caf40a374
DEMO:
http://hxend.com/uploadif/
博文归石头和博客园所有,转载请注明出处,方便更新。 |
http://www.cnblogs.com/webers/p/4162108.html |