douqie1884
douqie1884
2011-05-04 05:36

PHP脚本 - 检测是在linux还是Windows下运行?

已采纳

I have a PHP script that may be placed on a windows system or a linux system. I need to run different commands in either case.

How can I detect which environment I am in? (preferably something PHP rather than clever system hacks)

Update

To clarify, the script is running from the command line.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

14条回答

  • douxi3432 douxi3432 10年前

    Check the value of the PHP_OS constantDocs.

    It will give you various values on Windows like WIN32, WINNT or Windows.

    See as well: Possible Values For: PHP_OS and php_unameDocs:

    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        echo 'This is a server using Windows!';
    } else {
        echo 'This is a server not using Windows!';
    }
    
    点赞 评论 复制链接分享
  • duangu1878 duangu1878 3年前

    If you want to check if running under Linux, just test if (PHP_OS === 'Linux'). No need to use strtolower() and substr().

    点赞 评论 复制链接分享
  • doujiacai4986 doujiacai4986 2年前

    Starting with PHP 7.2.0 you can detect the running O.S. using the constant PHP_OS_FAMILY:

    if (PHP_OS_FAMILY === "Windows") {
      echo "Running on Windows";
    } elseif (PHP_OS_FAMILY === "Linux") {
      echo "Running on Linux";
    }
    

    See the official PHP documentation for its possible values.

    点赞 评论 复制链接分享
  • douyong1908 douyong1908 5年前
    function isWin(){
     if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || PHP_SHLIB_SUFFIX == 'dll' || PATH_SEPARATOR == ';') {
        return true;
     } else {
        return false;
     }
    }
    
    点赞 评论 复制链接分享
  • dtt83024 dtt83024 9年前

    This should work in PHP 4.3+:

    if (strtolower(PHP_SHLIB_SUFFIX) === 'dll')
    {
        // Windows
    }
    else
    {
        // Linux/UNIX/OS X
    }
    
    点赞 评论 复制链接分享
  • dongqiya9552 dongqiya9552 5年前

    You can check if a constant exists in PHP >5.3.0 (manual)

    if (defined('PHP_WINDOWS_VERSION_BUILD')) {
        // is Windows
    }
    

    Previously, this method was used in Symfony. Now they use a different method:

    if ('\\' === DIRECTORY_SEPARATOR) {
        // is Windows
    }
    
    点赞 评论 复制链接分享
  • douhao6557 douhao6557 4年前

    According to Predefined Constants: User Contributed Notes Volker's and rdcapasso solution, you can simply create helper class like this:

    <?php
    
    class System {
    
        const OS_UNKNOWN = 1;
        const OS_WIN = 2;
        const OS_LINUX = 3;
        const OS_OSX = 4;
    
        /**
         * @return int
         */
        static public function getOS() {
            switch (true) {
                case stristr(PHP_OS, 'DAR'): return self::OS_OSX;
                case stristr(PHP_OS, 'WIN'): return self::OS_WIN;
                case stristr(PHP_OS, 'LINUX'): return self::OS_LINUX;
                default : return self::OS_UNKNOWN;
            }
        }
    
    }
    

    Usage:

    if(System::getOS() == System::OS_WIN) {
      // do something only on Windows platform
    }
    
    点赞 评论 复制链接分享
  • duanbairan4235 duanbairan4235 10年前

    From http://www.php.net/manual/en/reserved.variables.server.php#102162 :

    <?php
    echo '<table border="1">';
    
    foreach ($_SERVER as $k => $v){
        echo "<tr><td>" . $k ."</td><td>" . $v . "</td></tr>";
    }
    
    echo "</table>"
    ?>
    

    This is the entire $_SERVER array... as ArtWorkAD has noted, by using the HTTP_USER_AGENT key, you can extract the OS more explicitly.

    点赞 评论 复制链接分享
  • douzuo0711 douzuo0711 10年前

    You can check if the directory seperator is / (for unix/linux/mac) or \ on windows. The constant name is DIRECTORY_SEPARATOR

    if (DIRECTORY_SEPARATOR == '/') {
        // unix, linux, mac
    }
    
    if (DIRECTORY_SEPARATOR == '\\') {
        // windows
    }
    
    点赞 评论 复制链接分享
  • douyijin7741 douyijin7741 10年前

    The php_uname function can be used to detect this.

    echo php_uname();
    
    点赞 评论 复制链接分享
  • dtbglv5450 dtbglv5450 10年前

    Core Predefined Constants: http://us3.php.net/manual/en/reserved.constants.php which has the PHP_OS (string) constant.

    Or if you want to detect the OS of the client:

    <?php
        echo $_SERVER['HTTP_USER_AGENT'] . "
    
    ";
    
        $browser = get_browser(null, true);
        print_r($browser);
    ?>
    

    From http://us3.php.net/manual/en/function.get-browser.php


    According to your edit you can refer to this dublicate PHP Server Name from Command Line

    You can use

    string php_uname ([ string $mode = "a" ] )
    

    So

    php_uname("s")
    

    's': Operating system name. eg. FreeBSD.

    Would do the trick for you, see here http://php.net/manual/en/function.php-uname.php

    点赞 评论 复制链接分享
  • dongshan2004 dongshan2004 9年前
    if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
        echo 'This is a server using Windows!';
    } else {
        echo 'This is a server not using Windows!';
    }
    

    seems like a bit more elegant than the accepted answer. The aforementioned detection with DIRECTORY_SEPARATOR is the fastest, though.

    点赞 评论 复制链接分享
  • dongsheng4126335 dongsheng4126335 8年前

    Note that PHP_OS reports the OS that PHP was built on, which is not necessarily the same OS that it is currently running on.

    If you are on PHP >= 5.3 and just need to know whether you're running on Windows or not-Windows then testing whether one of the Windows-specific constants is defined may be a good bet, e.g.:

    $windows = defined('PHP_WINDOWS_VERSION_MAJOR');
    
    点赞 评论 复制链接分享
  • duanbei7035 duanbei7035 5年前

    To detect whether it's Windows, OS X or Linux:

    if (stripos(PHP_OS, 'win') === 0) {
        // code for windows
    } elseif (stripos(PHP_OS, 'darwin') === 0) {
        // code for OS X
    } elseif (stripos(PHP_OS, 'linux') === 0) {
        // code for Linux
    }
    

    stripos is a bit slower than substr in this particular case, yet it's efficient enough for such a small task, and more elegant.

    点赞 评论 复制链接分享