0

I have a PHP code that needs to run in background (no cronjobs or similar options) that is complete, but I just can't call it in the background. I'm trying the following:

$files = $_GET['files'];
$id = $_GET['id'];

pclose(popen("start /B D:\\server\\php\\php.exe D:\\server\\www\\whatever\\importat\\import.php?files=".$files."&id=".$id." 2>nul >nul", "r"));

But it' never runs. If I go and run import.php manually, it works without fail.

PS: I can't change the import.php.

Help?

Ayan
  • 2,738
  • 3
  • 35
  • 76
Metal Sonic
  • 61
  • 1
  • 8
  • 2
    Can't you just run the script in the web? E.g. `file_get_contents("http://www.link.to.script.com/import.php?params")` – Charlotte Dunois Aug 26 '15 at 19:15
  • Does the command works if you run it in cmd? Try turning on error report to see if it returns any errors. – Anders Andersen Aug 26 '15 at 19:18
  • @AndersAndersen I'm trying to run the script in this way `D:/server/php/php-win.exe "D:/path/to/progress_import.php?files=9&id=2"`, and I receive no errors in CMD, but it don't work either. Using `file_get_contents`works, but don't runs in background, and that's the entire purpose of the script. – Metal Sonic Aug 27 '15 at 14:54
  • Using \ instead of / in the cmd don't change anything too. – Metal Sonic Aug 27 '15 at 15:00
  • @MetalSonic try looking, at first user contributed on this page http://php.net/manual/en/function.exec.php#86329 Its the one by "Arno van den Brink " Maybe you need to remove this "2>nul >nul" in your command – Anders Andersen Aug 27 '15 at 17:17
  • Did it, but nothing happened. I've got permission and edited the *import.php* and make the code runs like this: `$cmd = "D:\\path\\php.exe D:\\path\\progress_import.php"; pclose(popen("start /B ". $cmd, "a"));` But no success yet. – Metal Sonic Aug 27 '15 at 17:58

1 Answers1

0

I got the solution:

What I am doing is calling the PHP inside a batch and launching it using popen:

$phpBatch = "progress_import_".$idImport.".bat";
$myPhp = fopen($phpBatch, "w") or die("Error opening file!");
$batchContent .= "@echo OFF \n";
$batchContent .= "\"D:\\path\\to\\php.exe\" D:\\path\\to\\progress_import.php ".$idImport." ".$_SESSION['numberFiles']." ";
pclose(popen("start /B D:\\path\\to\\".$phpBatch."", "r"));

And inside the progress_import.php i'm using:

$id = $argv[1];
$numberFiles = $argv[2];

To catch the parameters.

Hope that helps someone.

Metal Sonic
  • 61
  • 1
  • 8