PhP 5.4 中的 PhP 上传进度不起作用.会话变量未设置

时间:2023-02-22
本文介绍了PhP 5.4 中的 PhP 上传进度不起作用.会话变量未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一开始就遇到了 PhP 文件上传进度监视器的问题.

I have a problem with PhP File Upload progress monitor at the very start.

首先,这里是相关的 PhP.ini 设置(指令、本地值和主值):

First, here are the relevant PhP.ini settings (Directive, Local Value and Master Value):

     session.upload_progress.cleanup    On  On
     session.upload_progress.enabled    On  On
     session.upload_progress.freq   1%  1%
     session.upload_progress.min_freq   1   1
     session.upload_progress.name   PHP_SESSION_UPLOAD_PROGRESS  PHP_SESSION_UPLOAD_PROGRESS
     session.upload_progress.prefix upload_progress_    upload_progress_

这是表格(简化版):

     <form id="fileupload" style="position:relative;" target="iframe_fileupload" action="http://www.athiyoga.org/testupload.php" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="<?echo ini_get("session.upload_progress.name");?>" value="first"/>
             <input type="file" name="file_1">
            <button type="submit" >Start Submit</button>
      </form>

我有 JQUERY Ajax 代码,在同一个 PhP 文件中(当然,作为一个 JS 脚本),如:

I have JQUERY Ajax code, in the same PhP file (of course, as a JS script), as in:

    $('#fileupload').submit(function(event){

    //UPDATED THIS PART after reading: http://stackoverflow.com/questions/19336610/delay-in-populating-session-upload-progress-data
    //POSTING the magic variable PHP_SESSION_UPLOAD_PROGRESS during status inquiry too

       var params = {PHP_SESSION_UPLOAD_PROGRESS:"first", some_var:20 };
       var data_params = jQuery.param( params );
       setTimeout(function(){
              upload_promise = $.ajax({
                url: 'upload_status.php', 
                data: data_params,
                dataType: 'html',
                type    : 'POST',
                cache   : false 
             });
             $.when(upload_promise).done(function(status_response){
                   $('#response_status').html(status_response);
             });
        },5000);
      ...
      ...

upload_status.php 只是回应 $_SESSION 数组.我还在 form-php 中设置了一个测试会话变量,以确保 AJAX(通过 upload_status.php)选择该会话变量.确实如此.但不是 $_SESSION 数组中上传状态的标志(无变量/索引)!文件被上传.我确保文件足够大,以便 5000 毫秒足以报告一些中间状态.

The upload_status.php simply echoes the $_SESSION array. I also set a test session variable in the form-php to make sure that the AJAX (thru upload_status.php) picks that session variable. It does. But not a sign (no variable/index) of the upload status in the $_SESSION array! The files get uploaded. I made sure that the files are big enough so that the 5000ms is sufficient to report some intermediate status.

我之前从未实现过 PhP 文件上传进度条,所以我想知道我是否遗漏了什么.一旦我在上传中获得一个状态点,我就可以完成其余的工作.

I have never implemented PhP file upload progress bar before so I wonder if I am missing something. Once I get one status-point in the upload, I will be able to do the rest.

谢谢

推荐答案

可能存在一些问题,我已经列出了其中的几个.

There could be some issues, I have listed down few of them.

  • 必须禁用 Web 服务器的请求缓冲才能使其正常工作,否则 PHP 可能仅在完全上传后才能看到文件上传.
  • 当您的网络服务器通过 FastCGI 运行 PHP 时,此功能不起作用.
  • 不要忘记,必须在生成表单之前初始化会话,否则您将无法在会话中获得任何信息.
  • 它不适用于 PHP 5.3 或更早版本.
  • 请注意,如果您运行该代码并打印出 $_SESSSION[$key] 的内容,您会得到一个空数组,因为 session.upload_progress.cleanup 默认是打开的,并且它会在所有 POST 后立即清除进度信息数据已读取.将其设置为 Off 或 0 以查看 $_SESSION[$key] 的内容.

这可以帮助您跟踪进度条http://pecl.php.net/package/uploadprogress

This can help you to track your progress bar http://pecl.php.net/package/uploadprogress

我希望这能帮助您找出问题所在.

I hope this will help you to dig out the problem.

这篇关于PhP 5.4 中的 PhP 上传进度不起作用.会话变量未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP 上传大小及其对帖子大小和内存限制的影响 下一篇:使用jquery上传图片

相关文章

最新文章