php文件上传进度条例子

Posted 哔哩哔哩干杯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php文件上传进度条例子相关的知识,希望对你有一定的参考价值。

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>Demo : PHP(5.4) Upload Progress via Session</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="author" content="">
    <meta name="robots" content="INDEX,FOLLOW" />

    <link rel="shortcut icon" href="/favicon.ico">
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="../assets/css/pureweber.css" rel="stylesheet">

<style type="text/css">
.progress{
    width:100%;
    border:1px solid #4da8fe;
    border-radius:40px;
    height:20px;
    position:relative;
}
.progress .label{
    position:relative;
    text-align:center;
}
.progress .bar{
    position:absolute;
    left:0;top:0;
    background:#4D90FE;
    height:20px;
    border-radius:40px;
    min-width:20px;
}
</style>

</head>
<body>
    <div id="nav" class="container">
        <div class="inner">
            <a href="/" class="logo">PureWeber</a>
        <ul>
            <li><a id="to-top" href="#nav">&laquo; 回到文章</a></li>
        </ul>
        </div>
    </div>
    <div id="wrap" class="container">

<div id="header">
    <h1>Demo : PHP(5.4) Upload Progress via Session</h1>
</div>
<div id="article">

<form id="upload-form" action="upload.php" method="POST" enctype="multipart/form-data" style="margin:15px 0" target="hidden_iframe">
    <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="test" />
    <p><input type="file" name="file1" /></p>
    <p><input type="submit" value="Upload" /></p>
</form>


<div id="progress" class="progress" style="margin-bottom:15px;display:none;">
    <div class="bar" style="width:0%;"></div>
    <div class="label">0%</div>
</div>

</div> <!-- #article -->

    <div id="footer">
        <p>Copyright &copy; 2012 PureWeber.com</p>
    </div>
</div><!-- #wrap -->


<iframe id="hidden_iframe" name="hidden_iframe" src="about:blank" style="display:none;"></iframe>
<script src="../../../jquery-1.11.2.min.js"></script>
<script type="text/javascript">
function fetch_progress(){
    $.get(\'progress.php\',{ \'<?php echo ini_get("session.upload_progress.name"); ?>\' : \'test\'}, function(data){
        var progress = parseInt(data);

        $(\'#progress .label\').html(progress + \'%\');
        $(\'#progress .bar\').css(\'width\', progress + \'%\');

        if(progress < 100){
            setTimeout(\'fetch_progress()\', 1000);
        }else{
            $(\'#progress .label\').html(\'完成!\');
        }
    }, \'html\');
}

$(\'#upload-form\').submit(function(){
    $(\'#progress\').show();
    setTimeout(\'fetch_progress()\', 1000);
});
</script>
</body>
</html>
View Code
<?php
session_start();

$i = ini_get(\'session.upload_progress.name\');

$key = ini_get("session.upload_progress.prefix") . $_GET[$i];

if (!empty($_SESSION[$key])) {
    $current = $_SESSION[$key]["bytes_processed"];
    $total = $_SESSION[$key]["content_length"];
    echo $current < $total ? ceil($current / $total * 100) : 100;
}else{
    echo 100;
}


?>
View Code
<?php

if(is_uploaded_file($_FILES[\'file1\'][\'tmp_name\'])){
    //unlink($_FILES[\'file1\'][\'tmp_name\']);
    move_uploaded_file($_FILES[\'file1\'][\'tmp_name\'], "./{$_FILES[\'file1\'][\'name\']}");
}

?>
View Code

 

以上是关于php文件上传进度条例子的主要内容,如果未能解决你的问题,请参考以下文章

PHP利用Session实现上传进度

PHP 使用进度条上传文件 - PHP和JQuery

php实现大文件上传带进度条

php 使用进度条上传文件

流程控制

简单的跨浏览器,带有进度条的 jQuery/PHP 文件上传 [关闭]