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

1

6

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: https://mcmap.net/q/752077/-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

Nathan answered 11/2, 2014 at 13:22 Comment(20)
Look into xhr upload progressWhereof
dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5Whereof
@dollarvar. Thats Pure HTML5 solution and does not give file-level status update. Though my example involves only 1 file upload, I am looking for a solution that supports multiple files and a separate progress bar for each file upload.Thanks anyway.Nathan
You looked into that rifht php.net/manual/en/session.upload-progress.phpWhereof
@dollarVar. I had read it and read it again after your message just to see if I missed anything. Still no luck. I had made a few mistakes but had corrected them earlier. I know it must be something simple. I did upvote your suggestions.Nathan
Ok, for what I can see, you cannot echo a whole array. The upload_status.php simply echoes the $_SESSION array. Also you have a mistake in you form. <?echo should be <?php echo or <?=. (If open short tags are enabled #201140)Whereof
I believe you have to force writing the session data while it running because it actually save it when the PHP finnish to execute the fileCudweed
I meant you cannot (sorry phone) "echo $array" but "echo $array[0]".Whereof
I think you should look in the link provided by dollarVar it says: 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. Servers such as Nginx are known to buffer larger requests. I think you can use #15029087 in case you are using apacheCudweed
@Cudweed There are two configuration issues. One is the web server's request buffering(as u say) and the other is the running of PhP as FastCGI and not as an Apache module. I keep calling and sending support calls/tickets to Godaddy.com, which is the hosting company and they keep giving me unrelated replies. No answer whatsoever to solve the main problem. When I upgraded, they messed up setting the temp folder which took a day to find and fix. The default temp directory for saving sessions did not exist (took time to detect) so I had to use a PhP function to use another... I m stuck now.Nathan
@dollarVar, I am using print_r(). Thanks for your observation though. I used "echo" in the generic sense. And if I was using echo in a syntactically wrong way, PhP would have reported an error and not displayed an empty array.Nathan
;) Actually it would have echoed Array and I wondered why you do not get that in the XHR response... (Update: Plus a Notice depending on your settings.)Whereof
As far as what I understand from googling FastCGI does not support this feature, You can try using client side solution: uploadify.com/documentation/uploadifive/onprogressCudweed
@Cudweed I have to get this feature implemented using PhP's file upload progress functions only. That said, your comments were useful and I have upvoted all of them. Can you throw some light on how I would do this: "I believe you have to force writing the session data while it running..." That makes sense to me but how do I do it?Nathan
@dollarVar. I am sure the problem I have has nothing to do with the syntax of Php echo or print_r statements. I am outputing $_FILES and $_SESSION with other indexes and they display fine. I have been doing this for long. The problem is not at this level. My choice of words may be technically inaccurate in describing the display of the arrays. Anyhow upvoted your comment as my words may have been misleading.Nathan
I THINK I GOT THE SOURCE OF THE PROBLEM. THE GODADDY WEBSERVER USES FASTCGI TO RUN PHP SO THAT EXPLAINS IT. THE PHP MANUAL (OR RATHER A USER COMMENT) CLEARLY SAYS THAT IT WILL NOT WORK UNDER FASTCGI. GODADDY SENT ME ALL KINDS OF SUPPORT MESSAGES EXCEPT THE ONE THAT WAS RELEVANT.Nathan
This is what I told you... "..from googling FastCGI does not support this feature" Any way you should try client side solution, or using different web server good luck!Cudweed
@talsibony. I tried the APC file upload progress approach and failed there too -:) I think I will take your advice and move to client side solution! Thanks for all the comments.Nathan
A bit offtopic, but "PhP" or "pHp" looks horrible for me :X And yes, for upload module / upload progress module you should use your own webserver as webhosting companies will not let you to achieve something like progress bar or resumable uploads. Maybe it could help you in future: I am using nginx + php5-fpm (FastCGI) and here I also could not use APC or other progress modules, had to use nginx upload progress module specially designed for these problems and uploading ~10GB files is no more problem.Swett
@WigglerJtag. Point well-taken about "PHP." On the real topic, I certainly learned a lot trying to get PHP upload progress bar to work. The fastCGI bottleneck... the setting config params in .htaccess to disable it... Yet no luck! I have often considered hosting my own web server locally in-house but got nervous about all the what-ifs. Just keep asking myself, is it worth it. May be the time has come to bite the bullet and do just that or find a hosting company that supports it neatly. Will look into nginx too. Thanks.Nathan
G
13

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.

Gypsy answered 18/2, 2014 at 10:57 Comment(1)
I have learned all the above issues the hard way. Nice to have it in one place. Have upvoted your answer. On second thoughts, just decided to go ahead and accept it as an answer too as it happens to be the answer anyway. Just edit your answer, if possible, to add APC approach which can work with PHP <5.4 but also requires that FastCGI be disable. Thanks.Nathan

© 2022 - 2024 — McMap. All rights reserved.