简体   繁体   中英

Does session upload progress work with nginx and php-fpm?

I'm running nginx 1.2.3 / php-fpm 5.4.6 and trying to use the Session upload progress feature. During an upload $_SESSION never contains any upload data whatsoever. At first I assumed coding errors, but even the most simple/basic upload progress tests failed to produce anything within $_SESSION . I now suspect that the file is being POSTed directly to nginx, which completely handles the upload from beginning to end, THEN nginx passes the upload on to php-fpm so quickly that there is no real "progress" to report. Am I correct in this assessment? If so, how can I get around this?

phpconfig confirms that the session.upload settings are all set correctly.

Below is the current test code, borrowed from this blog .

<?php
  /* File upload progress in PHP 5.4 */

  /* needs a 5.4+ version */
  $version = explode( '.', phpversion() );
  if ( ($version[0] * 10000 + $version[1] * 100 + $version[2]) < 50400 )
    die( 'PHP 5.4.0 or higher is required' );

  if ( !intval(ini_get('session.upload_progress.enabled')) )
    die( 'session.upload_progress.enabled is not enabled' );

  session_start();

  if ( isset( $_GET['progress'] ) ) {

    $progress_key = strtolower(ini_get("session.upload_progress.prefix").'demo');

    if ( !isset( $_SESSION[$progress_key] ) ) exit( "uploading..." );

    $upload_progress = $_SESSION[$progress_key];
    /* get percentage */
    $progress = round( ($upload_progress['bytes_processed'] / $upload_progress['content_length']) * 100, 2 );

    exit( "Upload progress: $progress%" );
  }
?>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<?php if ( isset($_GET['iframe']) ): /* thank you Webkit... */ ?>
<form action="" method="POST" enctype="multipart/form-data">
  <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="demo">
  <input type="file" name="uploaded_file">
  <input type="submit" value="Upload">
</form>

<script type="text/javascript">
window.location.hash = ""; /* reset */
jQuery("form").bind("submit", function() { window.location.hash = "uploading"; });
</script>

<?php else: ?>

<iframe src="?iframe" id="upload_form"></iframe>
<script type="text/javascript">
  jQuery(document).ready(init);

  function init() {
    /* start listening on submit */
    update_file_upload_progress();
  }

  function update_file_upload_progress() {
    if ( window.frames.upload_form.location.hash != "#uploading" ) {
      setTimeout( update_file_upload_progress, 100 ); /* has upload started yet? */
      return;
    }
    $.get( /* lather */
      "?progress",
      function(data) {
        /* rinse */
        jQuery("#file_upload_progress").html(data);
        /* repeat */
        setTimeout( update_file_upload_progress, 500 );
      }
    ).error(function(jqXHR, error) { alert(error); });
  }
</script>

<div id="file_upload_progress"></div>
<?php endif; ?>

A note in the documentation page on php.net says :

s.zarges 19-Jun-2012 09:32
Note, this feature doesn't work, when your webserver is runnig PHP via FastCGI. There will be no progress informations in the session array.

Unfortunately PHP gets the data only after the upload is completed and can't show any progress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM