【问题标题】:Does session upload progress work with nginx and php-fpm?会话上传进度是否适用于 nginx 和 php-fpm?
【发布时间】:2012-09-20 23:34:54
【问题描述】:

我正在运行 nginx 1.2.3 / php-fpm 5.4.6 并尝试使用 Session upload progress 功能。在上传期间$_SESSION 绝不包含任何上传数据。起初我假设编码错误,但即使是最简单/基本的上传进度测试也无法在$_SESSION 中产生任何内容。 我现在怀疑该文件被直接发布到 nginx,它完全处理从头到尾的上传,然后 nginx 将上传传递到 php-fpm 如此之快,以至于没有真正的“进度”要报告。我在这个评估中正确吗?如果是这样,我该如何解决这个问题?

phpconfig 确认 session.upload 设置都设置正确。

下面是当前的测试代码,借自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; ?>

【问题讨论】:

    标签: file-upload nginx php


    【解决方案1】:

    php.net 文档页面中的注释说:

    s.zarges 2012 年 6 月 19 日 09:32
    请注意,当您的网络服务器通过 FastCGI 运行 PHP 时,此功能不起作用。会话数组中不会有进度信息。

    很遗憾PHP只有上传完成后才能获取数据,无法显示任何进度。

    【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多