PHP Session Upload Progress

teynon edited this page Apr 3, 2013 · 12 revisions

PHP 5.4 will likely introduce built-in support for server-side upload progress status:

The following code snippets outline how to use this feature with the File Upload plugin:

JavaScript (application.js)

// This code assumes each file upload has a related DOM node
// set as data.context, which is true for the UI version:
$('#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 value:
        var progressObj = {
            name: 'PHP_SESSION_UPLOAD_PROGRESS',
            value: (new Date()).getTime()  // pseudo unique ID
        // Start the progress polling:'interval', setInterval(function () {
            $.get('progress.php', $.param([progressObj]), function (result) {
                // Trigger a fileupload progress event,
                // using the result as progress data:
                e = $.Event( 'progress', {bubbles: false, cancelable: true});
                $.extend(e, result);
                ($('#fileupload').data('blueimp-fileupload') ||
                    $('#fileupload').data('fileupload'))._onProgress(e, data);
            }, 'json');
        }, 1000)); // poll every second
}).bind('fileuploadalways', function (e, data) {

PHP (progress.php)

  // Assuming default values for session.upload_progress.prefix
  // and
  $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);

APC Alternative

According to, PHP APC is available for PHP 5.2+. When installed and configured properly, APC file upload progress can be enabled for older browsers with the File Upload Extension located at this fork.

To enable apc with this extension, simply set the apc option.

    apc: true