Windows/php pclose 和 popen 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7042591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:49:55  来源:igfitidea点击:

Windows/php pclose and popen issue

phpwindowsiispopenpclose

提问by Naftali aka Neal

Here is my code:

这是我的代码:

<?php
    require_once 'dbconnect.php';
    function execInBackground($cmd) { 
        if (substr(php_uname(), 0, 7) == "Windows"){ 
            pclose(popen("start /B ". $cmd, "r"));  
        } 
        else { 
            exec($cmd . " > /dev/null &");   
        } 
    } 

    if(isset($_GET['date'])){
        //CHECK LOCK
        $checkLock = "Select IS_FREE_LOCK('overnight') as `lock`;";
        $result = mysql_query($checkLock) or die(mysql_error());
        while($information = mysql_fetch_array($result)){
            if($information['lock'] == 0){
                die('Overnight is already running, please try again later.');
            }
        }
        execInBackground("php overnightQueries.php {$_GET['date']}");
        //echo "<pre>".print_r($output2, true)."</pre>";
        header('Refresh: 3; url=index.php');
        die('running queries...');
    }
    else {

        die('PLEASE SET DATE');

    }
?>

I am using a windows machine.

我正在使用 Windows 机器。

I get the following warnings:

我收到以下警告:

Warning: popen(start /B php overnightQueries.php 2011_08_12,r): No error in C:\inetpub\GTSA\runOvernight.php on line 5

警告:popen(start /B php nightQueries.php 2011_08_12,r):第 5 行 C:\inetpub\GTSA\runOvernight.php 中没有错误

AND:

和:

Warning: pclose() expects parameter 1 to be resource, boolean given in C:\inetpub\GTSA\runOvernight.php on line 5

警告:pclose() 期望参数 1 是资源,布尔值在第 5 行 C:\inetpub\GTSA\runOvernight.php 中给出

回答by Marc B

$handle = popen("start /B ". $cmd, "r");  
if ($handle === FALSE) {
   die("Unable to execute $cmd");
}
pclose($handle);

popen returns false if there was a problem, which you blindly pass to pclose, hence the second error.

如果出现问题,则 popen 返回 false,您盲目地将其传递给 pclose,因此出现第二个错误。

As for the first error, check that PHP is in your environment's path - you may need to specify an absolute path to php.exe.

对于第一个错误,请检查 PHP 是否在您的环境路径中 - 您可能需要指定 php.exe 的绝对路径。