0

I'm trying to implement multi-file upload script via JavaScript and PHP.

I use session.upload_progress to track the progress and when ever a file finished uploading i copy it from the tmp folder to permanent location and register it on the data base. Finally the actual script to which the files was submitted will copy and register the remaining files and finish the operation.

So now i try to detect connection lost or the the user abortion(via xhr.abort) on server side to reverse those changes (made by session.upload_progress) :delete the already moved files and remove them from the DB.

The problem is that i don't know how to detect the loose of connection or the abortion i use this code to make some logs to see what happens but it loging nothing:

    ignore_user_abort(true);
    if(isset($_POST['option'])){
        if($_POST['option'] == "picture_upload_form") upload_pictures_form();
        elseif($_POST['option'] == "picture_upload"){ // this part is what is important here
            if(connection_status() != CONNECTION_NORMAL) {
                error_log("connection_status() : " .connection_status());
                error_log( print_r($_SESSION, TRUE));
                exit();
            }
            upload_pictures(); // when no problem this function will save the remaining files normally
        }
        elseif($_POST['option'] == "picture_upload_progress") upload_pictures_progress();
    }

This code handels the requests and calls different function depending on the posted option:

  1. option = picture_upload_form: will send to the client the upload form where the user can choose his files
  2. option = picture_upload_progress : will turn back (echo) the upload progress state
  3. option = picture_upload : this is what will be called when submitting the files. it should handle the remaining files correctly if no problem or delete those already treated (by session.upload_progress) if there was timeout or abortion (it does the first part correctly but not the second and I got no log when I abort or disconnect the server).

Edit: For test purposes I changed the value to ignore_user_abort = 1 in php.ini, disabled the upload progress tracking (the only request send to server now is the files submitting) and added the following code to the top of the script:

ignore_user_abort(true);
error_log("connection_status() : " .connection_status());

I cleared php_error.log, then i submit the files as normal wait a bit and before the upload is finished i disconnect the server from client. thought after a while (i suppose it the timeout delay) i find this in the log: connection_status() : 0

Thank you in advance

fekiri malek
  • 354
  • 1
  • 3
  • 14
  • 1
    How is your PHP script starting before the upload is finished? AFAIK, all the files have to be uploaded so that PHP can fill in all the entries in `$_FILES`. Are you using some other mechanism to upload the files? – Barmar Aug 19 '16 at 01:02
  • I'm using a FormData to which I append the files, i add too: name="option" value = "picture_upload" the formdata's action points to that same script above; so when it loads the files the function upload_pictures() is called and it handles the remaining files from ($_FILES); i save the already treated files (by session.upload_progress) as array in the $_SESSION variable. the whole code is some 300 lines too long to be posted here. – fekiri malek Aug 19 '16 at 02:25
  • Most of the code before are just functions to be called by that if-else code; there is also session starting declaration and some general variables and functions (included by require_once). – fekiri malek Aug 19 '16 at 02:29
  • I added some echo("..."); flush(); ob_flush(); before checking the state but I still get 0 for both connection_status() and connection_aborted() for what ever i abort the upload (xhr.abort()) or disconnect the server. Any suggestion? – fekiri malek Aug 19 '16 at 20:00

0 Answers0