3

nginx 1.2.3 / php-fpm 5.4.6を実行していて、セッションアップロードの進行状況機能を使用しようとしています。アップロード中$_SESSIONは、アップロードデータが含まれることはありません。最初はコーディングエラーを想定していましたが、最も単純で基本的なアップロードの進行状況テストでさえ、内で何も生成できませんでした$_SESSION。ファイルがnginxに直接POSTされているのではないかと思います。これは、アップロードを最初から最後まで完全に処理します。その後、nginxはアップロードをphp-fpmにすばやく渡すため、実際の「進行状況」を報告することはできません。この評価は正しいですか?もしそうなら、どうすればこれを回避できますか?

phpconfigは、session.upload設定がすべて正しく設定されていることを確認します。

以下は、このブログから借用した現在のテストコードです。

<?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; ?>
4

1 に答える 1

1

php.netのドキュメントページのメモには次のように書かれています。

s.zarges 19-Jun-2012 09:32
WebサーバーがFastCGIを介してPHPを実行している場合、この機能は機能しないことに注意してください。セッション配列には進行状況情報はありません。

残念ながら、PHPはアップロードが完了した後にのみデータを取得し、進行状況を表示できません。

于 2012-09-20T09:42:04.513 に答える