1

Basically in a nutshell is I am using exec() to run WinSCP, which is causing the script to hold until the file is uploaded. Is there anyway to make it so that the script continues, and the upload runs in the background?

Running PHP 5.3.1 on Win7.

hakre
  • 193,403
  • 52
  • 435
  • 836
Tyler
  • 11
  • 1
  • 1
    In other words: you want multithreading in PHP? Now you know the keyword: http://stackoverflow.com/search?q=php+multithreading – BalusC Jan 24 '10 at 06:11
  • It's not multithreading in the classical way, i.e. having multiple execution lines of your program, usually sending messages to each other. He just want to spawn another program in the background. It's a lot simpler way of multithreading, if you want to call it that way. – Sebastián Grignoli Jan 04 '12 at 12:26

3 Answers3

1
$pipe = popen("/bin/bash ./some_shell_script.sh argument_1","r");

Works both under Linux and Windows.

(just don't close the pipe and don't try to read from it)

Your PHP script will continue to run while the process spawned through popen runs in the background. When your script reaches the end of file or a call to die(), it will wait for the other process to finish and the pipe to close before quitting completely.

Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
0

Will this function from the PHP Manual help?

function runAsynchronously($path,$arguments) {
    $WshShell = new COM("WScript.Shell");
    $oShellLink = $WshShell->CreateShortcut("temp.lnk");
    $oShellLink->TargetPath = $path;
    $oShellLink->Arguments = $arguments;
    $oShellLink->WorkingDirectory = dirname($path);
    $oShellLink->WindowStyle = 1;
    $oShellLink->Save();
    $oExec = $WshShell->Run("temp.lnk", 7, false);
    unset($WshShell,$oShellLink,$oExec);
    unlink("temp.lnk");
}

Taken from PHP on a windows machine; Start process in background then Kill process.

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • That didn't seem to work. I ran that, and I've tried all the examples from here as well http://www.somacon.com/p395.php – Tyler Jan 26 '10 at 15:46
0

PHP isn't designed to run as a separate thread so apache has to wait for it to complete the exec. This being the case, your script could run into time and memory constraints during an exec call which would make it a less than optimal solution to user exec. Generally not recommended.

Why not use the PHP functions for ftp? http://www.php.net/manual/en/function.ftp-get.php

user250120
  • 901
  • 1
  • 6
  • 8