Asked  8 Months ago    Answers:  5   Viewed   41 times

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

 Answers

54

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.

Wednesday, March 31, 2021
 
Daveel
answered 8 Months ago
34

You can use the HTML5 dragenter and dragleave events to create a dropzone.
Then by placing a file input inside the dropzone, and hiding it with CSS, you can upload the file when the change event for the input fires, like this

var dropzone = $("#dropzone"),
    input    = dropzone.find('input');

dropzone.on({
    dragenter : dragin,
    dragleave : dragout
});

input.on('change', drop);

function dragin(e) { //function for drag into element, just turns the bix X white
    $(dropzone).addClass('hover');
}

function dragout(e) { //function for dragging out of element                         
    $(dropzone).removeClass('hover');
}

function drop(e) {
    var file = this.files[0];
    $('#dropzone').removeClass('hover').addClass('dropped').find('img').remove();

    // upload file here
}

FIDDLE

Wednesday, March 31, 2021
 
devo
answered 8 Months ago
60

SOLUTION: after many hours of searching, it seems this problem was occurring when I access my website without adding the "www." before the domain. so what actually was happening is, I was logging in with mydomain.com/login.php sets session somewhere, that my member control doesn't recognize, so it redirects me back to www.mydomain.com/login.php, that when I login everything works Ok.

when I login from www.mydomain.com/login.php (with the www.) it logs in correctly from first attemp.

So I added a code to make sure I always have the www in the URL:

if ($_SERVER['HTTP_HOST'] == "mydomain.com")
{
   $url = "http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header("Location: $url");
} 

and everything works well now. Hope it helps someone.

Wednesday, March 31, 2021
 
LoicTheAztec
answered 8 Months ago
71

Here is a example of what Michael is talking about. http://www.phpletter.com/Our-Projects/AjaxFileUpload/

Saturday, May 29, 2021
 
Classified
answered 5 Months ago
69

The include() function does not access the file via HTTP, it accesses the file through the OS's own file system. So GET variables are not counted. (as they are not part of the file name).

In layman's terms, the point of include is to "copy/paste" all the contents on one file to another on the file, so that you don't have one gigantic file, but a few smaller, more maintainable ones.

Tuesday, August 17, 2021
 
Sergio Carneiro
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :