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

2019-03-26 01:47发布

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 new lines in textdocuments.

I'm using a CSV file in windows but each time I think I add a new line, what really happens is the new line gets pasted to the back of the latest line.

Reason is, in windows, a new line is this: \r\n And the CSVHandler.class.php file I'm using only adds \n

However, in MAC OS X that's the new line, which is different from windows.

So I'm looking for this so I can implement a simple if() statement and solve this. Currently I've hardcoded the \r\n, but it should be simpler, no?

13条回答
太酷不给撩
2楼-- · 2019-03-26 01:54

You could use the predefined constant PHP_OS.

I'm using

if (PHP_OS === 'WINNT') {...}

查看更多
对你真心纯属浪费
3楼-- · 2019-03-26 01:55

Check the $_SERVER variable.

echo "<pre>";
print_r($_SERVER);

You can then use strstr (or any string comparison function) to check if you are on Windows. In this example, I checked the SERVER_SIGNATURE but you can use whatever key you want.

$isWindows = strstr($_SERVER[SERVER_SIGNATURE], "Win32") !== FALSE;
查看更多
smile是对你的礼貌
4楼-- · 2019-03-26 01:59

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"));
}
查看更多
地球回转人心会变
5楼-- · 2019-03-26 02:03

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

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-03-26 02:07

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

ARRRGH! PLEASE STOP PERPETUATING THIS MYTH!

Mac OS 9 used that like 10 years ago, but no one uses OS9 anymore. MACS USE UNIX LINE ENDINGS. \n. "Mac" used today should refer to contemporary computers, just as "Windows" refers to XP or vista unless otherwise qualified.

Saying Macs use \r is about as correct as saying that "Windows runs on top of MS-DOS, supports only the FAT16 filesystem, and has no 64-bit support."

Nobody should ever ever use \r for anything under any circumstances. Unless they are targeting old-ass macs.

查看更多
三岁会撩人
7楼-- · 2019-03-26 02:07

You may also want to do a php info call to have a look at a lot of the configuration settings on your PHP setup, code is simple:

phpinfo();
查看更多
登录 后发表回答