为了尝试后台测试,我创建了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将在我的项目的文件夹中创建文件。
我该如何解决这个问题?
https://stackoverflow.com/questions/50866646
复制相似问题