weixin_33695450 2014-10-15 07:44 采纳率: 0%
浏览 28

AJAX的PHP和Bash结果

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 -> |
                                           |                     |
                                            _____________________
  • 写回答

1条回答 默认 最新

  • weixin_33691598 2014-10-16 06:10
    关注

    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().

    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?