如何获取运行 PHP 的操作系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1482260/
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-08-25 02:46:05  来源:igfitidea点击:

How to get the OS on which PHP is running?

phpoperating-system

提问by Martin Klepsch

For building a unix/dos specific script I need to know on which kind of operating system I am.

为了构建特定于 unix/dos 的脚本,我需要知道我使用的是哪种操作系统。

How do i get this information?
phpinfo();tells me a lot more and not very clear whether I'm running on unix or not.

我如何获得这些信息?
phpinfo();告诉我更多并且不太清楚我是否在 unix 上运行。

回答by Pascal MARTIN

PHP has many predefined constantsthat are often useful.

PHP 有许多预定义的常量,它们通常很有用。

Here, PHP_OSis the one you are looking for.

在这里,PHP_OS就是你要找的人。


For instance, on my current machine, this code :


例如,在我当前的机器上,此代码:

var_dump(PHP_OS);

Gives :

给出:

string 'Linux' (length=5)


You have some examples and comparisons with what the php_unamefunction can get you on the manual page of php_uname; for instance (quoting):


您有一些示例和与该php_uname功能可以在手册页上为php_uname您提供的内容的比较;例如(引用)

<?php
echo php_uname();
echo PHP_OS;

/* Some possible outputs:
Linux localhost 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686
Linux

FreeBSD localhost 3.2-RELEASE #15: Mon Dec 17 08:46:02 GMT 2001
FreeBSD

Windows NT XN1 5.1 build 2600
WINNT
*/

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

That page also says :

该页面还说:

For the name of just the operating system, consider using the PHP_OSconstant, but keep in mind this constant will contain the operating system PHP was builton.

对于刚刚操作系统的名称,可以考虑使用PHP_OS恒定的,但要记住这个常量将包含操作系统PHP被上。

回答by spikyjt

Just bear in mind that PHP_OSactually contains the platform on which PHP was built. This may not be the same platform as that on which it is deployed. Therefore php_uname('s')is more reliable.

请记住,它PHP_OS实际上包含构建 PHP 的平台。这可能与部署它的平台不同。因此php_uname('s')更可靠。

回答by shery089

As of PHP 7.2.0we have a new Predefined Constant to get the operating system family i.e. PHP_OS_FAMILY. It returns a stringEither of 'Windows', 'BSD', 'OSX', 'Solaris', 'Linux' or 'Unknown'.

PHP 7.2.0 开始,我们有一个新的预定义常量来获取操作系统系列,即PHP_OS_FAMILY。它返回一个字符串“Windows”、“BSD”、“OSX”、“Solaris”、“Linux”或“未知”。

回答by jmucchiello

PHP_OS is prefined with the host os name: http://us2.php.net/manual/en/reserved.constants.php

PHP_OS 以主机操作系统名称为前缀:http://us2.php.net/manual/en/reserved.constants.php

回答by M.J.Ahmadi

There are 2 different way to check the platform that your PHP is running on it.

有两种不同的方法可以检查您的 PHP 在其上运行的平台。

  1. Using PHP_OSwhich is a const and will point to the 'operating system name' that your PHP was built in it.
  2. Using PHP built in function php_uname()that will tell you more about platform (Operating system name, Host name, Version information, Release name, Machine type) that your script is running on it.
  1. 使用PHP_OSwhich 是一个常量,将指向您的 PHP 内置的“操作系统名称”。
  2. 使用 PHP 内置函数php_uname()会告诉您有关脚本在其上运行的平台(操作系统名称、主机名、版本信息、版本名称、机器类型)的更多信息。

回答by Ahmad

PHP Does not provide any function to get the name of the distribution, php_unameis similar to Linux command uname, does not provide any info about the distribution itself.

PHP 不提供任何获取发行版名称的函数,php_uname类似于 Linux 命令uname,不提供有关发行版本身的任何信息。

Neither php_unamenor PHP_OSgive sufficient info. about the distribution but the OS type (e.g. Linux / Windows).

既没有php_uname也没有PHP_OS提供足够的信息。关于发行版,但与操作系统类型有关(例如 Linux / Windows)。

I think the best way to know what is the running OS/distribution is to read /etc/os-release, the good thing is this file has read permission for all system users and the bad thing is it may not work on shared hosting.

我认为了解正在运行的操作系统/发行版是什么的最好方法是 read /etc/os-release,好消息是这个文件对所有系统用户都有读权限,坏消息是它可能无法在共享主机上运行。

Here I wrote a very simple PHP function which reads and convert os-releaseto an array:

在这里,我编写了一个非常简单的 PHP 函数,它读取并转换os-release为数组:

    function getOSInformation()
    {
        if (false == function_exists("shell_exec") || false == is_readable("/etc/os-release")) {
            return null;
        }

        $os         = shell_exec('cat /etc/os-release');
        $listIds    = preg_match_all('/.*=/', $os, $matchListIds);
        $listIds    = $matchListIds[0];

        $listVal    = preg_match_all('/=.*/', $os, $matchListVal);
        $listVal    = $matchListVal[0];

        array_walk($listIds, function(&$v, $k){
            $v = strtolower(str_replace('=', '', $v));
        });

        array_walk($listVal, function(&$v, $k){
            $v = preg_replace('/=|"/', '', $v);
        });

        return array_combine($listIds, $listVal);
    }

This function prints something like this:

此函数打印如下内容:

Array
(
    [name] => Ubuntu
    [version] => 16.04.2 LTS (Xenial Xerus)
    [id] => ubuntu
    [id_like] => debian
    [pretty_name] => Ubuntu 16.04.2 LTS
    [version_id] => 16.04
    [home_url] => http://www.ubuntu.com/
    [support_url] => http://help.ubuntu.com/
    [bug_report_url] => http://bugs.launchpad.net/ubuntu/
    [version_codename] => xenial
    [ubuntu_codename] => xenial
)

Held og lykke [1] ;-)

举行 og lykke [1] ;-)

[1] Danish phrase means good luck.

[1] 丹麦语意为好运。

回答by fitorec

On php 7.2.0 you can use the PHP_OS_FAMILYconstant:

在 php 7.2.0 上,您可以使用PHP_OS_FAMILY常量:

In other PHP version you can use:

在其他 PHP 版本中,您可以使用:

/**
* return DOS OR UNIX
*/
function familyOS() {
   return (stripos(PHP_OS, "WIN") === 0)? "DOS" : "UNIX";
}

回答by helvete

#!/usr/bin/env php
<?php
$platform = DIRECTORY_SEPARATOR === '\'
    ? 'Windows'
    : 'Unix/Linux';

I am aware that this is not very granular, but it may suffice for a simple recognition between Win and *nix systems. YMMV

我知道这不是很细粒度,但它可能足以在 Win 和 *nix 系统之间进行简单的识别。青年会

回答by rahul

$user_agent     =   $_SERVER['HTTP_USER_AGENT'];
function getOS() { 

    global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}

$user_os  =   getOS();


$device_details =   "<strong>Operating System: </strong>".$user_os."";

print_r($device_details);