【问题标题】:PECL Uploadprogress Will Not Give Me Upload ProgressPECL Uploadprogress 不会给我上传进度
【发布时间】:2012-06-24 22:32:34
【问题描述】:

我正在尝试使用 PECL uploadprogress 扩展来实现一个非常基本的 AJAX 上传进度条。我找到了适用于所有浏览器的示例代码:http://svn.php.net/viewvc/pecl/uploadprogress/trunk/examples/。它使用 iframe 将更新写入。我想获取更新并做一些 jquery 来构建进度条。这是我的代码(我知道我没有写代码来说明上传结束的时间)client.php:

<?php
$id = md5(microtime() . rand());
?>

<!DOCTYPE html>
<html>

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
        function getProgress(){
            $.get("progress.php", {"ID":'<?php echo $id ?>'}, function(data){
                console.log(data);
            });
            window.setTimeout(getProgress(), 5000);
        }
</script>

<body>
    <form onsubmit="getProgress()" target="_self" enctype="multipart/form-data" method="post">
        <input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" />
        <label>Select File:</label>
        <input type="file" name="file" />
        <br/>
        <label>Select File:</label>
        <input type="file" name="file2" />
        <br/>
        <label>Upload File:</label>
        <input id="submitButton" type="submit" value="Upload File" />
    </form>
</body>
</html>

还有progress.php:

<?php
if (function_exists("uploadprogress_get_info")) {

    $info = uploadprogress_get_info($_GET['ID']);
} else {
    $info = false;
}

$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;

echo $progress;

我出错了,打印出来的都是 0。有什么想法吗?

【问题讨论】:

  • PHP 是作为 Apache 模块还是 cgi 运行? PECL 上传进度仅适用于 mod_php。
  • 我已经确认使用 iframe 的演示代码确实有效。我正在尝试将其精简到最基本的要求。

标签: php jquery ajax upload progress


【解决方案1】:

尝试替换

$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;

$progress = ($info['bytes_uploaded']*100)/$info['bytes_total']; 

$info['bytes_uploaded']$info['bytes_total'] 都是整数,因此除法不是浮点数而是向下舍入为整数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2013-01-04
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多