Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhP Upload progress in PhP 5.4 is not working. Session variables not set

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

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_

Here is the form (simplified):

     <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>

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);
      ...
      ...

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.

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.

Thanks

like image 634
Sunny Avatar asked Feb 11 '14 13:02

Sunny


Video Answer


1 Answers

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

  • The web server's request buffering has to be disabled for this to work properly, else PHP may see the file upload only once fully uploaded.
  • This feature doesn't work, when your webserver is running PHP via FastCGI.
  • Don't forget, that the session has to be initialized before the form is generated, otherwise you will get no information in the session.
  • It won't work in PHP 5.3 or earlier.
  • Note that if you run that code and you print out the content of $_SESSSION[$key] you get an empty array due that session.upload_progress.cleanup is on by default and it cleans the progress information as soon as all POST data has been read. Set it to Off or 0 to see the content of $_SESSION[$key].

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.

like image 176
Navigator Avatar answered Sep 18 '22 15:09

Navigator