php execute a background process

前端 未结 19 1446
萌比男神i
萌比男神i 2020-11-21 07:04

I need to execute a directory copy upon a user action, but the directories are quite large, so I would like to be able to perform such an action without the user being aware

19条回答
  •  旧巷少年郎
    2020-11-21 07:39

    I'd just like to add a very simple example for testing this functionality on Windows:

    Create the following two files and save them to a web directory:

    foreground.php:

    loading page
    "; function run_background_process() { file_put_contents("testprocesses.php","foreground start time = " . time() . "\n"); echo "
      foreground start time = " . time() . "
    "; // output from the command must be redirected to a file or another output stream // http://ca.php.net/manual/en/function.exec.php exec("php background.php > testoutput.php 2>&1 & echo $!", $output); echo "
      foreground end time = " . time() . "
    "; file_put_contents("testprocesses.php","foreground end time = " . time() . "\n", FILE_APPEND); return $output; } echo "
    calling run_background_process
    "; $output = run_background_process(); echo "
    output = "; print_r($output); echo "
    "; echo "
    end of page
    "; ?>

    background.php:

    
    

    Give IUSR permission to write to the directory in which you created the above files

    Give IUSR permission to READ and EXECUTE C:\Windows\System32\cmd.exe

    Hit foreground.php from a web browser

    The following should be rendered to the browser w/the current timestamps and local resource # in the output array:

    loading page
    calling run_background_process
      foreground start time = 1266003600
      foreground end time = 1266003600
    output = Array
    (
        [0] => 15010
    )
    end of page
    

    You should see testoutput.php in the same directory as the above files were saved, and it should be empty

    You should see testprocesses.php in the same directory as the above files were saved, and it should contain the following text w/the current timestamps:

    foreground start time = 1266003600
    foreground end time = 1266003600
    background start time = 1266003600
    background end time = 1266003610
    

提交回复
热议问题