首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP如何执行后台任务?

PHP如何执行后台任务?
EN

Stack Overflow用户
提问于 2018-06-15 06:12:41
回答 2查看 2K关注 0票数 1

为了尝试后台测试,我创建了3个文件:

Index.html (负责通过ajax调用php文件)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Background Task Manager</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>

Press The Button To execute a Background Task...
<br>
<button id="perform">Perform Task</button>

</body>

<script>

$( document ).ready(function() {

    $( "#perform" ).click(function() {
      submitAjax();
    });

    function submitAjax() {
            $.ajax({
                url: 'test.php',
                type: "post",
                data: '',
                success: function (data) {
                    alert(data);
                }
            });
        }

});

</script>

</html>

Test.php (使用后台方法调用另一个文件的文件)

<?php

//Perform Background Task

exec("C:/wamp/bin/php/php5.6.35/php.exe C:/wamp/www/background/file.php");

echo "Process Started";

?>

File.php (将在后台执行的文件)

<?php

//Create a File

sleep(20);

$content = "My Text File";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);

echo "File Created...";

?>

的想法如下:一旦用户单击按钮,就会向test.php文件发出一个请求。test.php将触发对file.php的后台请求,消息('Process Started')将立即出现,20秒后将在我的项目文件夹中创建一个文件。

正在发生什么:当用户单击按钮时,我只在20秒后收到消息'Process Started‘,即请求不是在后台模式下发出的。

我希望发生的事情:当用户点击按钮时,消息‘进程已启动’将立即出现,20秒后,php将在我的项目的文件夹中创建文件。

我该如何解决这个问题?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50866646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档