To try a background test, I create 3 files:
Index.html (Responsible for calling a php file by ajax)
<!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 (The file who call another file using a background method)
<?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 (The file that will be executed in background)
<?php
//Create a File
sleep(20);
$content = "My Text File";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
echo "File Created...";
?>
The idea would be as follows: Once the user clicks the button, a request is made to the test.php file. The test.php will trigger a background request for the file.php, the message ('Process Started') will appear immediately, and 20 seconds after a file would be created in my project folder.
What is happening: When the user clicks the button, I only receive the message 'Process Started' 20 seconds later, ie the request is not being made in background mode.
What I wish to happen: When the user clicks the button, the message 'Process Started' will appear immediately, and 20 seconds later php will create the file in the folder of my project.
How can I solve this problem ?