0

im creating sort of system to copy files via web interface on my linux machine.

What i have is a bash script, php page and html page which is making an AJAX request to PHP page.

So here is an example of data flow in this system

HTML Page Form With AJAX -> PHP Script -> exec('./copy.sh '.$_REQUEST['folder'].' '.$_REQUEST['target'].'', $Output, $Result); echo end($Output); -> HTML Page Status div

Here is PHP part of Exec function:

exec('./copy.sh '.$_REQUEST['folder'].' '.$_REQUEST['target'].'', $Output, $Result); 
echo end($Output);

Here is my copy.sh

#!/bin/bash
InitialDir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
Folder=$1
TargetDir=$2
cd "${InitialDir}"
rsync -a ${Folder} ${TargetDir} && echo "Complete!"

The thing is, i want to add progressbar to this operation, and it works with this code:

$('#CopyFiles').click(function(){
overlaystart();
var url = "/copy.php";
$.ajax({
    type: "POST",
    url: url,
    data: {folder: Folder, target: TargetFolder},
    success: function(data)
    {

    }
});

setTimeout(function(){
    closestart();
    window.location = '/';
}, 9000);


$('.progressbarstart').animate(
    {width:'100%'}, 
    {
        duration:8000,
        step: function(now, fx) {
            if(fx.prop == 'width') {
                $(this).html(Math.round(now * 100) / 100 + '%');
            }
        }
    }        
);
});

But here is coming a problem, AJAX woks perfectly, php script works with no problems, but the thing is, even if files copied faster than 9 seconds (progressbar animation time), the overlay is still on, but sometimes files will be copied longer than 9 seconds.

So what im asking about is:

  1. Is it possible to somehow monitor copy progress with php + ajax (not mandatory)
  2. Is it possible to make AJAX close window even if its 4s left to animate progressbar, but received "Completed!" response from bash
  3. if non of the above mentioned possible, what else can i do?

P.S. Here is a small description of what im trying to achieve:

AJAX to PHP -> Execute Bash File -> Wait For Response -> Is Completed?
                                           ^            |
                                           |            -> Yes -> Close Modal Window Overlay
                                           |            |
                                           |            -> No -> |
                                           |                     |
                                            _____________________
Ivan Zhivolupov
  • 1,107
  • 2
  • 20
  • 39
  • Can you post the exec part of your php code? You could use `popen()` and read your scripts output, then post back information? Check [this answer](http://stackoverflow.com/a/20109859/824495) for an idea. – mehmetseckin Oct 15 '14 at 07:58
  • @Seçkin I think i've already posted my bash file, and exec file aswell (first code tags). popen is good, but if i'll query my php file again (with ajax), it will start new copying process – Ivan Zhivolupov Oct 15 '14 at 08:04

1 Answers1

0

We use the next schema. At long operations on server (but it is not the copying of files) on your step "Waiting Response" we start periodical ajax requests to watch process execution. The main process cancel the execution time limit. The data for progress bar (and the command cancel by user, possible) we tried to exchange data through user's session between main process and ajax-watchers. We had problem with blocking of session data, we did not know about session_write_close(); and made the data exchanging through db, but this is bad decision and should be refactored to sessions with session_write_close().

Solo.dmitry
  • 690
  • 8
  • 15