会话上传进度是否与nginx和php-fpm一起工作? [英] Does session upload progress work with nginx and php-fpm?

查看:201
本文介绍了会话上传进度是否与nginx和php-fpm一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行nginx 1.2.3 / php-fpm 5.4.6并尝试使用会话上传进度功能。在上传过程中, $ _ SESSION 永远不会包含任何上传数据。起初,我假定编码错误,但即使是最简单/基本的上传进度测试,也无法在 $ _ SESSION 内产生任何东西。
我现在怀疑这个文件是直接发送给nginx的,它完全处理了从头到尾的上传过程,于是nginx把这个上传过程传递给了php-fpm,所以没有真正的进度报告。我在这个评估中是否正确?如果是这样,我怎么能解决这个问题?



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



下面是当前的测试代码,从
$ b

 <?php 
/ * PHP 5.4中的文件上传进度* /

/ *需要5.4+版本* /
$ version = explode('。',phpversion()); (($ version [0] * 10000 + $ version [1] * 100 + $ version [2])<50400)
die('需要PHP 5.4.0或更高版本')的
。 ;
$ b $ 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];
/ *获得百分比* /
$ progress = round(($ upload_progress ['bytes_processed'] / $ upload_progress ['content_length'])* 100,2);

exit(上传进度:$ progress%);
}
?>

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

<?php if(isset($ _ GET ['iframe'])):/ *谢谢Webkit ... * /?>
< form action =method =POSTenctype =multipart / form-data>
< input type =hiddenname =<?php echo ini_get(session.upload_progress.name);?>值= 演示 >
< input type =filename =uploaded_file>
< input type =submitvalue =上传>
< / form>

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

<?php else:?>

< iframe src =?iframeid =upload_form>< / iframe>
< script type =text / javascript>
jQuery(document).ready(init);
$ b函数init(){
/ *开始监听提交* /
update_file_upload_progress();


函数update_file_upload_progress(){
if(window.frames.upload_form.location.hash!=#uploading){
setTimeout(update_file_upload_progress,100 ); / *已经开始上传了吗? * /
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; ?>





s.zarges 19-Jun-2012 09:32

请注意,这个功能不起作用,当你的web服务器通过FastCGI运行PHP。会话数组中没有任何进度信息。


不幸的是PHP只有在上传完成后才能获取数据,不能显示任何进展。

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.

这篇关于会话上传进度是否与nginx和php-fpm一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆