PHP Ajax上传进度条

Was*_* A. 19 javascript php ajax upload progress-bar

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input name="uploaded" type="file" />
<input type="submit" value="Upload" />
</form>

<?php
if(isset($_REQUEST['submit'])){
   $target = "data/".basename( $_FILES['uploaded']['name']) ;
   move_uploaded_file($_FILES['uploaded']['tmp_name'], $target);
}
?>
Run Code Online (Sandbox Code Playgroud)

我非常了解Javascript,AJAX和JQuery等,我相信可以使用PHP,AJAX和Javascript等创建上传进度条.

我很惊讶如何在上传过程中获取上传的大小(意味着我想知道的每一秒,上传了多少文件以及剩余多少,我认为应该可以使用AJAX等)文件.

这是PHP手册的链接,但我不明白:http: //php.net/manual/en/session.upload-progress.php

有没有其他方法使用PHP和AJAX显示上传进度条但不使用PHP的任何外部扩展?我无权访问php.ini

Bab*_*aba 37

介绍

它说,PHP Doc非常详细

当上载正在进行时,以及当将session.upload_progress.name INI设置的同名变量设置为POST时,上传进度将在$ _SESSION超全局中可用.当PHP检测到此类POST请求时,它将填充$ _SESSION中的数组,其中索引是session.upload_progress.prefix和session.upload_progress.name INI选项的连接值.通常通过读取这些INI设置来检索密钥,即

您需要的所有信息都已准备好在PHP会话命名中

  • 开始时间
  • CONTENT_LENGTH
  • bytes_processed
  • 文件信息(支持多个)

您只需要提取此信息并将其显示在HTML表单中即可.

基本例子

a.html

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
    var intval = null;
    var percentage = 0 ;
    function startMonitor() {
        $.getJSON('b.php',
        function (data) {
            if (data) {
                percentage = Math.round((data.bytes_processed / data.content_length) * 100);
                $("#progressbar").progressbar({value: percentage});
                $('#progress-txt').html('Uploading ' + percentage + '%');

            }
            if(!data || percentage == 100){
                $('#progress-txt').html('Complete');
                stopInterval();
            }
        });
    }

    function startInterval() {
        if (intval == null) {
            intval = window.setInterval(function () {startMonitor()}, 200)
        } else {
            stopInterval()
        }
    }

    function stopInterval() {
        if (intval != null) {
            window.clearInterval(intval)
            intval = null;
            $("#progressbar").hide();
            $('#progress-txt').html('Complete');
        }
    }

    startInterval();
</script>
Run Code Online (Sandbox Code Playgroud)

b.php

session_start();
header('Content-type: application/json');
echo json_encode($_SESSION["upload_progress_upload"]);
Run Code Online (Sandbox Code Playgroud)

PHP会话上载进度的示例

这是PHP Session Upload Progress中更好的优化版本

JavaScript的

$('#fileupload').bind('fileuploadsend', function (e, data) {
    // This feature is only useful for browsers which rely on the iframe transport:
    if (data.dataType.substr(0, 6) === 'iframe') {
        // Set PHP's session.upload_progress.name value:
        var progressObj = {
            name: 'PHP_SESSION_UPLOAD_PROGRESS',
            value: (new Date()).getTime()  // pseudo unique ID
        };
        data.formData.push(progressObj);
        // Start the progress polling:
        data.context.data('interval', setInterval(function () {
            $.get('progress.php', $.param([progressObj]), function (result) {
                // Trigger a fileupload progress event,
                // using the result as progress data:
                e = document.createEvent('Event');
                e.initEvent('progress', false, true);
                $.extend(e, result);
                $('#fileupload').data('fileupload')._onProgress(e, data);
            }, 'json');
        }, 1000)); // poll every second
    }
}).bind('fileuploadalways', function (e, data) {
    clearInterval(data.context.data('interval'));
});
Run Code Online (Sandbox Code Playgroud)

progress.php

$s = $_SESSION['upload_progress_'.intval($_GET['PHP_SESSION_UPLOAD_PROGRESS'])];
$progress = array(
        'lengthComputable' => true,
        'loaded' => $s['bytes_processed'],
        'total' => $s['content_length']
);
echo json_encode($progress);
Run Code Online (Sandbox Code Playgroud)

其他例子