Detect if a PHP script is being run interactively or not

前端 未结 3 464
-上瘾入骨i
-上瘾入骨i 2020-12-13 03:14

I have a script that is designed to be run both as a web page, and via the console.

Detecting which method was used to invoke the script seems pretty straight forwar

3条回答
  •  清歌不尽
    2020-12-13 03:21

    For Windows, you can use this:

    $b = stream_isatty(STDIN);
    if ($b) {
       echo "php script.php\n";
    } else {
       echo "php script.php < input_file\n";
    }
    

    https://php.net/function.stream-isatty

提交回复
热议问题