I have a script a.php and I want to call a (long running) script b.php from a.php on my Windows machine.
This is sample file a.php:
<?php
$command = 'start /B php b.php > NUL';
pclose(popen($command, 'r'));
?>
and this is sample file b.php
<?php
sleep(30);
?>
but this doesn't work, a.php waits till b.php is finished.
What can I do to run b.php in the background?
UPDATE
Maybe the problem is not related to how I call b.php but how a.php is called. Here is a more complete example of what I'm doing:
A user calls webservice index.php?cmd=download&source=s1 (I test it with Postman)
index.php:
<?php
include_once 'a.php';
if($_GET['cmd'] == "download") {
$source = $_GET['source'];
$flag = startDownload($source);
if($flag) {
echo("Download was initialized");
} else {
echo("Download not started");
}
}
?>
a.php:
<?php
function startDownload($source) {
if($source == "s1") {
$command = 'start /B php b.php > NUL';
pclose(popen($command, 'r'));
} else {
$command = 'start /B php c.php > NUL';
pclose(popen($command, 'r'));
}
return true;
}
?>
b.php:
<?php
$ch = curl_init();
$source = "http://example.com/bigfile.zip";
curl_setopt($ch, CURLOPT_URL, $source);
$data = curl_exec($ch);
curl_close($ch);
$file = fopen("/", "w+");
fputs($file, $data);
fclose($file);
?>