Does PHP have a function to detect the OS it's running on?

后端 未结 13 2427
北荒
北荒 2021-02-14 17:00

I wouldn\'t know under what keyword to look for this in the PHP database, so I\'m asking here.

Reason I want to know is because of how different Operating Systems handle

相关标签:
13条回答
  • 2021-02-14 17:23

    I'd recommend php_uname. Just read the explanation from the docs:

    php_uname — Returns information about the operating system PHP is running on

    I did like the suggestion of PHP_OS above, but be careful, per this note from the docs:

    consider using the PHP_OS constant, but keep in mind this constant will contain the operating system PHP was built on.

    0 讨论(0)
  • Also, try this function:

    $b = get_browser(null, true);
    

    and in $b['platform'] will be OS.

    BTW, *nix OS use \n as new line. Mac usees \r, Windows - \r\n

    0 讨论(0)
  • 2021-02-14 17:29

    Lots of answers aleady, but here is my 2cents:

    function windows_server()
    #   Purpose:    Check if server is Windows
    {
        return in_array(strtolower(PHP_OS), array("win32", "windows", "winnt"));
    }
    
    ## --------------------------------------------------------
    
    function linux_server()
    #   Purpose:    Check if server is Linux
    {
        return in_array(strtolower(PHP_OS), array("linux", "superior operating system"));
    }
    
    0 讨论(0)
  • 2021-02-14 17:29

    Probably the safest thing to do when reading is to determine the line ending character(s) from the file itself, or accept all line endings interchangeably. This protects you from harm if you copy the csv file from one machine to another with a different OS. If you read before writing, you can make your output line endings match the line endings you identified when reading.

    For CSV files, php has some library functions. Try searching php.net for fgetcsv and fputcsv. There is the auto_detect_line_endings which can be set in the php.ini, but I don't know the specifics of how it works.

    I always use the "\n" by itself on both linux and windows. I use notepad to edit them in windows and it doesn't break the endings. For my own use of csv I find that it's too much hassle to support different endings, but if it's something users have to interact with then you want to be safe rather than convenient.

    0 讨论(0)
  • 2021-02-14 17:33

    PHP has included the constant PHP_EOL for solving the problem you face, available since php 4.3.10 and PHP 5.0.2 - it contains a suitable end-of-line sequence for the server that PHP is running on.

    If you want to use a different end-of-line sequence suitable for a particular client, then you'll have to code that yourself. One way to determine the client OS is to use get_browser, assuming your server has an up-to-date browscap.ini

    0 讨论(0)
  • 2021-02-14 17:34

    As far as I know, if PHP is running on Windows, there will be an environment variable named WINDIR. I think that you could do this:

    $isWindows = isset($_SERVER['WINDIR']);
    
    0 讨论(0)
提交回复
热议问题