0

I am trying to implement a very basic AJAX upload progress bar using the PECL uploadprogress extension. I have found this sample code which works across all browsers: http://svn.php.net/viewvc/pecl/uploadprogress/trunk/examples/. It uses iframes to write the updates to. I would like to get the updates and do some jquery to build a progress bar. Here is my code (I know I did not write in code to account for when the upload ends) 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>

And 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;

I error out and all that prints is 0's. Any ideas?

Julian
  • 325
  • 1
  • 3
  • 10
  • Is PHP running as Apache module or cgi? PECL upload progress will only work with mod_php. – c2h5oh Jun 22 '12 at 15:46
  • 1
    I have confirmed that the demo code using iframes does work. I am trying to trim it down to the bare essentials. – Julian Jun 22 '12 at 15:50

1 Answers1

1

Try replacing

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

with

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

Both $info['bytes_uploaded'] and $info['bytes_total'] are integers, so division is not a float but is rounded down to a integer.

c2h5oh
  • 4,492
  • 2
  • 23
  • 30