For building a unix/dos specific script I need to know on which kind of operating system I am.
How do i get this information?
phpinfo();
tells me a lot more and not very clear whether I'm running on unix or not.
For building a unix/dos specific script I need to know on which kind of operating system I am.
How do i get this information?
phpinfo();
tells me a lot more and not very clear whether I'm running on unix or not.
I am aware that this is not very granular, but it may suffice for a simple recognition between Win and *nix systems. YMMV
There are 2 different way to check the platform that your PHP is running on it.
PHP_OS
which is a const and will point to the 'operating system name' that your PHP was built in it.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.PHP_OS is prefined with the host os name: http://us2.php.net/manual/en/reserved.constants.php
As of PHP 7.2.0 we have a new Predefined Constant to get the operating system family i.e. PHP_OS_FAMILY. It returns a string Either of 'Windows', 'BSD', 'OSX', 'Solaris', 'Linux' or 'Unknown'.
PHP Does not provide any function to get the name of the distribution,
php_uname
is similar to Linux commanduname
, does not provide any info about the distribution itself.Neither
php_uname
norPHP_OS
give sufficient info. about the distribution but the OS type (e.g. 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.Here I wrote a very simple PHP function which reads and convert
os-release
to an array:This function prints something like this:
Held og lykke [1] ;-)
[1] Danish phrase means good luck.