uploadify和七牛云存储的结合

widuu 2013-11-14 PHP技术 3,564 10 喜欢 (0)

演示名称

这个就是一个简单的实例,因为我的VPS的带宽不够大,有时候需要上传文件,太慢了,所以用的七牛,然后服务器安装了一个软件,每五分钟从七牛下载一次,这样做到了两边的同步,上传速度也上去了,东西都是现成的看我们怎么使用了!

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>文件上传</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="jquery.uploadify.js" type="text/javascript"></script>
<!--
    这个加载的是uploadify的js和css等,怎么用uploadify上传到七牛,然后再服务器做定时处理或者用软件定时下载到服务器上,解决带宽不够大
    上传速度慢的问题
-->
<link rel="stylesheet" type="text/css" href="uploadify.css">
<style type="text/css">
body {
    font: 13px Arial, Helvetica, Sans-serif;
    text-align:center; 
    width:500px;
    margin:0 atuto;
}
#file_upload{
         left:38%;
         margin-top:30px;
         margin:0 atuto;}
.uploadify-queue-item{
        margin-left:70px;
}
.queue-item {
    margin-left: 60px;
}
.swfupload{
    padding-right:30px;
    right: -30px;
}
</style>
</head>
<body>
    点击“浏览”按钮,选择您要上传的文档文件后,系统将自动上传并在完成后提示您。
    <p style="color:red;font-weight: bold;">请将文件压缩成ZIP或者RAR格式,否则可能会上传失败</p>
    <form>
        <div id="queue"></div>
        <input id="file_upload" name="file" type="file" multiple="true">
    </form>
    <label id="notice"></label>
    <?php
    require_once("./qnsdk/io.php");
    require_once("./qnsdk/rs.php");
    require_once("./qnsdk/fop.php");
    $bucket = "空间名称";
    $accessKey = '七牛key';
    $secretKey = '七牛key';
    Qiniu_SetKeys($accessKey, $secretKey);
    $putPolicy = new Qiniu_RS_PutPolicy($bucket);
    $upToken = $putPolicy->Token(null);
    ?>
    <script type="text/javascript">
        //保留两位小数
        function formatFloat(src, pos)
        {
           return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
        }
        //计算尺寸
        function size(filesize)
        {
            var size = null;
            if(filesize/1048756 < 1){
                size=formatFloat(filesize/1024, 2)+"KB";
            }else if(filesize/1073747824<1){
                size=formatFloat(filesize/1048756, 2)+"MB";
            }else{
                size=formatFloat(filesize/1073747824, 2)+"GB";
            }
            return size;
        }
        $(function() {
            var filetype = null;
            var filesize = null;
            var now=new Date();
            $('#file_upload').uploadify({
                'onSelect' : function(file){
                    filetype = file.type;
                    $('#file_upload').uploadify('settings', 'formData', {"key":"<?php echo date("Ymd-His") . '-' . rand(10000,99999);?>"+filetype}) //上传文件的名称
                },
                'fileObjName' : 'file',  //七牛的上传的字段名称是file
                'formData'     : {
                    'token'     : '<?php echo $upToken;?>', //token
                },

                'buttonText'  : '选择文件',
                'swf'      : 'uploadify.swf',
                'uploader' : 'http://up.qiniu.com/',
                'method'   : 'POST',
                'onUploadSuccess' : function(file, data, response) {
                      filesize = size(file.size);
                      window.top.$('#softsize').val(filesize);
                      var data = $('#file_upload').uploadify('settings','formData');
                      window.top.$('#test').val(data.key);
                    if(data.key!==""){
                        $("#notice").html("<font style='color:#73b304;bolid;font:700 14px Arial, Helvetica, sans-serif;'>上传成功!重新上传请单击选择文件按钮!</font>");
                    }else{
                        $("#notice").html("<font style='color:red;bolid;font:700 14px Arial, Helvetica, sans-serif;'>上传失败,请联系管理员!</font>");
                    }
                 },
            });
        });
    </script>
</body>
</html>

上边是一个上传的文件,下边是显示空间文件的下载地址的,然后用软件抓取下载就可以了!

<?php
    $password = "myfengtuzhi123";
    $passwrod = md5($password);
    $showpass = trim($_GET['show']);
    if(empty($_GET['show'])&& md5($showpass)!==$password)die('ERROR 404');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>文件列表</title>
</head>
<body>
<?php
    require_once("./qnsdk/io.php");
    require_once("./qnsdk/rs.php");
    require_once("./qnsdk/fop.php");
    require_once("./qnsdk/rsf.php");
    $bucket = "空间名称";
    $accessKey = '七牛key';
    $secretKey = '七牛key';
    Qiniu_SetKeys($accessKey, $secretKey);
    $client = new Qiniu_MacHttpClient(null);
    $result = array();
        $data = Qiniu_RSF_ListPrefix($client,$bucket,'','',5);
    do{
        if($data[1]==""){
            $marker = '';
        }else{
            $marker = $data[1];
        }
        $data = Qiniu_RSF_ListPrefix($client,$bucket,'',$marker,5);
        $marker = $data[1];
        foreach ($data[0] as $key => $value) {
            array_push($result,array($value["key"],$value["putTime"]));
        }
    }while($data[1]!=="");
    $len = count($result);
    //冒泡按时间排序
    for($i=1;$i<$len;$i++)
    {
        for($j=$len-1;$j>=$i;$j--)
            if($result[$j][1]<$result[$j-1][1])
            {
             $x=$result[$j];
             $result[$j]=$result[$j-1];
             $result[$j-1]=$x;
            }
    }
?>
<ul>
    <?php
        $time = date("Ymd");
        foreach ($result as $k => $v) {
            $puttime = explode("-",$v[0]);
            if($time - $puttime[0] <7){
                echo "<li><a href='http://widuuweb.u.qiniudn.com/{$v[0]}'>{$v[0]}</a></li>";
            }
        }
    ?>
<ul>
</body>
</html>

转载请注明来自微度网络,本文标题:《uploadify和七牛云存储的结合》

喜欢 0 发布评论
Top