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:
- option = picture_upload_form: will send to the client the upload form where the user can choose his files
- option = picture_upload_progress : will turn back (echo) the upload progress state
- 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