如何在PHP执行批处理文件时显示加载图像gif或消息


How to display a loading image gif or message while PHP is executing a batch file?

我一直在网上搜索答案,但找不到。

当长时间执行的脚本正在运行时,我如何显示一些加载消息或gif。我测试了许多不同的方法,比如javascript,因为我的脚本试图使用PLINK.exe来跟踪文件,大约需要30秒才能返回值。因为这是一行代码,我不能使用flush(),有其他方法可以实现吗?

<?php
$runCommand  = "C:'wamp'www'TS'batch'plink.exe Sever -l User '"ssh User@Server 'tail -600 /serverlog/test.log''" ";
$results=system($runCommand);
//exec($runCommand, $results); 
echo $results;
?>

我尝试了以下代码,这些代码正是我想要的。页面在加载php脚本时显示一个"loading.gif",并在脚本完成时将其隐藏。这是在使用JQuery


//$tr_arname=$_REQUEST['arname'] -> is the variable i've got from previous PHP page.
<body onload="loadingAjax('myDiv');">
<script>
var arname="<?php $tr_arname=$_REQUEST['arname']; echo "$tr_arname"; ?>";
function loadingAjax(div_id)
{
    $("#"+div_id).html('<center><img src="images/loading.gif"><br><br><font color="#006699" face="arial" size="4"><b>Loading arerror.log <br><?php echo "$tr_arname"; ?> <br>Please Wait ...</b></font></center>');
    $.ajax({
        type: "POST",
        url: "ThePHPScriptPage.php",
        data: "arname=" + arname,
        success: function(msg){
            $("#"+div_id).html(msg);
        }
    });
}
</script>
<div id="myDiv"></div>
</body>

您可以使用AJAX从另一个网页请求您的php文件。然后让javascript显示loading.gif,直到服务器响应请求。

您可以使用jQuery:http://api.jquery.com/load/

您不能仅使用PHP来完成此操作,因为它是服务器端执行(页面/脚本在用户可以在浏览器上看到任何内容之前就在服务器中呈现)

您应该将脚本放在一个单独的文件中,然后执行AJAX调用。

要显示加载图像,请执行以下操作(需要jQuery框架)

http://jquery-howto.blogspot.com/2009/04/display-loading-gif-image-while-loading.html

// SHOW YOUR LOADING GIF HERE
$('#result').load('yourscript.php', function() {
 //HIDE YOUR LODAING GIF HERE
});