0

Is there a way to find out which release of Ubuntu is installed with php?

on the console I would use

cat /etc/lsb-release 

and would get for example:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=13.10
DISTRIB_CODENAME=saucy
DISTRIB_DESCRIPTION="Ubuntu 13.10"
pjs
  • 18,696
  • 4
  • 27
  • 56
rubo77
  • 19,527
  • 31
  • 134
  • 226
  • So you want to write a PHP snippet that reports the OS details of the machine it's run on? Did I get that right? – Tieson T. Jan 15 '14 at 04:18
  • 1
    See also http://stackoverflow.com/questions/1482260/how-to-get-the-os-on-which-php-is-running. I'm not sure if it would show you the exact Ubuntu release, but you could probably figure it out based on the kernel version (`php_uname('r');`). – Mike Jan 15 '14 at 04:19

3 Answers3

3

You can parse /etc/lsb-release for info.

<?php
$release_info = parse_ini_file("/etc/lsb-release");
echo $release_info["DISTRIB_ID"];
echo $release_info["DISTRIB_RELEASE"];
echo $release_info["DISTRIB_DESCRIPTION"];
?>
codefreak
  • 6,950
  • 3
  • 42
  • 51
  • That's way better than `php_uname` as long as there's no sort of open_basedir restrictions. – Mike Jan 15 '14 at 04:33
2

This would collect a lot of useful data:

function get_release_info(){
    if(!$r = @parse_ini_file("/etc/lsb-release")){
        if(!$r = @parse_ini_file("/etc/os-release")){
            $r = @parse_ini_file("/etc/redhat-release");
        }
    }
    /*for ex:
        $r["DISTRIB_ID"]=Ubuntu
        $r["DISTRIB_RELEASE"]=13.10
        $r["DISTRIB_CODENAME"]=saucy
        $r["DISTRIB_DESCRIPTION"]="Ubuntu 13.10"
    */
    $r["debian_version"]=trim(@file_get_contents("/etc/debian_version")); // for ex. "wheezy/sid"
    $r["PHP_OS"]=PHP_OS;
    $r["SERVER_SOFTWARE"]=@$_SERVER["SERVER_SOFTWARE"];
    $r["osx_system_arch"]=@$_SERVER["_system_arch"];// Mac-specific for ex.=> x86_64
    $r["osx_system_version"]=@$_SERVER["_system_version"];// Mac-specific for ex.=> 10.9
    /**
        'a': This is the default. Contains all modes in the sequence "s n r v m".
        's': Operating system name. eg. FreeBSD.
        'n': Host name. eg. localhost.example.com.
        'r': Release name. eg. 5.1.2-RELEASE.
        'v': Version information. Varies a lot between operating systems.
        'm': Machine type. eg. i386.
    */
    $r["kernel_release_name"]=php_uname('r');
    $r["os_name"]=php_uname('s');
    $r["uname_version_info"]=php_uname('v');
    $r["machine_type"]=php_uname('m');
    $r["php_uname"]=php_uname();

    if(stristr($r["uname_version_info"],"Ubuntu")){
        // source: http://wiki.ubuntuusers.de/Kernel/Linux-Versionsnummern#Versionsnummern-herausfinden
        $distribution["4.10"]=array("Warty Warthog", "2.6.8");
        $distribution["5.04"]=array("Hoary Hedgehog", "2.6.10");
        $distribution["5.10"]=array("Breezy Badger", "2.6.12");
        $distribution["6.06"]=array("Dapper Drake", "2.6.15");
        $distribution["6.10"]=array("Edgy Eft", "2.6.17");
        $distribution["7.04"]=array("Feisty Fawn", "2.6.20");
        $distribution["7.10"]=array("Gutsy Gibbon", "2.6.22");
        $distribution["8.04"]=array("Hardy Heron", "2.6.24");
        $distribution["8.10"]=array("Intrepid Ibex", "2.6.27");
        $distribution["9.04"]=array("Jaunty Jackalope", "2.6.28");
        $distribution["9.10"]=array("Karmic Koala", "2.6.31");
        $distribution["10.04"]=array("Lucid Lynx", "2.6.32");
        $distribution["10.10"]=array("Maverick Meerkat", "2.6.35");
        $distribution["11.04"]=array("Natty Narwhal", "2.6.38");
        $distribution["11.10"]=array("Oneiric Ocelot", "3.0");
        $distribution["12.04"]=array("Precise Pangolin", "3.2"); #Backports: 3.5, 3.8, 3.11, 3.13
        $distribution["12.10"]=array("Quantal Quetzal", "3.5");
        $distribution["13.04"]=array("Raring Ringtail", "3.8");
        $distribution["13.10"]=array("Saucy Salamander", "3.11");
        $distribution["14.04"]=array("Trusty Tahr", "3.13"); # Backports 3.16, 3.19
        $distribution["14.10"]=array("Utopic Unicorn", "3.16");
        $distribution["15.04"]=array("Vivid Vervet", "3.19");
        $distribution["15.10"]=array("Wily Werewolf", "4.2");
        $distribution["16.04"]=array("Xenial Xerus", "4.4");

        foreach($distribution as $distribution=>$name_kernel){
            list($name,$kernel)=$name_kernel;
            if(version_compare($r["kernel_release_name"],$kernel,'>=')) {
                $r["ubuntu_distribution_by_kernel"]=$distribution;
                $r["ubuntu_distribution_name_by_kernel"]=$name;
                $r["ubuntu_distribution_shortname_by_kernel"]=strtolower(preg_replace("/ .*$/","", $name));
            }
        }
        if(empty($r["DISTRIB_RELEASE"]) and !empty($r["ubuntu_distribution_by_kernel"])) {
            $r["DISTRIB_RELEASE"]=$r["ubuntu_distribution_by_kernel"];
            $r["DISTRIB_DESCRIPTION"]="Ubuntu ".$r["ubuntu_distribution_by_kernel"];
            $r["DISTRIB_CODENAME"]=$r["ubuntu_distribution_shortname_by_kernel"];
        }
    }
    return $r;
}
rubo77
  • 19,527
  • 31
  • 134
  • 226
  • It would be nice to even better [interpret the output of php_uname](http://stackoverflow.com/questions/21129737/how-do-i-interpret-the-output-of-php-uname) – rubo77 Jan 15 '14 at 05:30
0

The $_SERVER[] can help.

On my mac I have

$_SERVER["_system_arch"] => x86_64
$_SERVER["_system_version"] => 10.9

I would recommend this to reading the file in /etc/lsb-release.

You can find more information here

Stephane Paquet
  • 2,315
  • 27
  • 31