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

Sunny
  • 9,245
  • 10
  • 49
  • 79
  • Look into xhr upload progress – loveNoHate Feb 11 '14 at 17:02
  • 1
    http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/ – loveNoHate Feb 11 '14 at 17:05
  • @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. – Sunny Feb 11 '14 at 17:11
  • 1
    You looked into that rifht http://www.php.net/manual/en/session.upload-progress.php – loveNoHate Feb 11 '14 at 18:00
  • 1
    @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. – Sunny Feb 11 '14 at 18:26
  • 2
    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. ` – loveNoHate Feb 12 '14 at 07:15
  • 1
    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 file – talsibony Feb 12 '14 at 11:07
  • I meant you cannot (sorry phone) "echo $array" but "echo $array[0]". – loveNoHate Feb 12 '14 at 11:09
  • 1
    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 http://stackoverflow.com/questions/15028587/prevent-output-buffering-with-php-and-apache in case you are using apache – talsibony Feb 12 '14 at 11:18
  • @talsibony 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. – Sunny Feb 12 '14 at 13:31
  • @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. – Sunny Feb 12 '14 at 13:33
  • ;) 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.) – loveNoHate Feb 12 '14 at 13:47
  • 2
    As far as what I understand from googling FastCGI does not support this feature, You can try using client side solution: http://www.uploadify.com/documentation/uploadifive/onprogress/ – talsibony Feb 12 '14 at 14:14
  • @talsibony 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? – Sunny Feb 12 '14 at 14:37
  • @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. – Sunny Feb 12 '14 at 14:40
  • 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. – Sunny Feb 12 '14 at 14:50
  • 1
    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! – talsibony Feb 12 '14 at 17:00
  • @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. – Sunny Feb 12 '14 at 17:17
  • 1
    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. – Wiggler Jtag Feb 15 '14 at 15:11
  • @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. – Sunny Feb 16 '14 at 08:20

1 Answers1

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.

Navigator
  • 430
  • 6
  • 14
  • 2
    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. – Sunny Feb 18 '14 at 12:52