0

I've a php script that pushes large media files to a remote server via FTP. This is working smoothly. I would like to enhance this and make it report back to a ajax client that initiates the transfers. Basically, report back something like below after each transfer:

1/12: File xbacd.dat has been copied successfully to /dir1/sub2/ - 2.2Mb

2/12: File impact_analy.ppt was not found in /var/www/files - Resuming.

3/12: File abc_consult.doc has been copied successfully to /dir1/sub3/ - 8.2Mb

Ajax handler on the other hand should be displaying this in a div, as and when it arrives.

I had no luck with the following code, and hence switching over to AJAX.

    ob_start();

    require_once 'init.php';

    while(files){

    // Transfer code

    // status
    echo "$idx/$count: File $f has been copied successfully to $tar_dir - $s";

    ob_flush;

flush;

}
mishka
  • 677
  • 6
  • 20
  • 1
    Check out [comet](http://en.wikipedia.org/wiki/Comet_(programming)) for long polling in javascript. – Mike Aug 26 '13 at 06:27
  • 1
    Also possibly handy for you http://stackoverflow.com/questions/6093103/jquery-ajax-display-data-as-it-comes-in – vollie Aug 26 '13 at 06:29

1 Answers1

1

I'll tell you what I did once, I'm no sure it's the best solutions but it worked. You should have an iFrame and set the target of the form to the iFrame

<script>
function showMsg(msg){
   alert(msg);
}
</script>
<form action="theCommandHandler.php" method="post" target="myIframe">
  <input type="submit" value="Do A Command" />
</form>

<iframe name="myIframe"></iframe>

once posted to theCommandHandler.php, you should have a line of code like this in your php script:

echo "<script>parent.showMsg('$idx/$count: File $f has been copied successfully to $tar_dir - $s');</script>";

You can send as many messages as you like back to client and they will invoke the showMsg function on the parent document.

noamtcohen
  • 4,432
  • 2
  • 20
  • 14