本文介绍了准确确定运行PHP的操作系统类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定PHP服务器正在运行的OS的类型.按类型,我指的是诸如"windows"或"linux"之类的字符串,而不是"wince","winnt"等.

I need to determine the type of OS the PHP server is running on.By type, I mean strings like "windows" or "linux", not "wince", "winnt" etc.

到目前为止,我不得不指出:PHP_OS和uname(),后者比以前的版本更可靠(PHP_OS表示根据PHP建立的操作系统-根据文档).

So far, I have to leads: PHP_OS and uname(), the later being more reliable than the earlier (PHP_OS says what OS PHP was built on - according to documentation).

推荐答案

重要的是要知道,没有非Windows OS字符串将不包含文本"win",也没有非OSX OS字符串将不包含文本.单词"darwin",依此类推.检测操作系统很容易.

It's important to know that no non-Windows OS string is going to contain the text "win", and no non-OSX OS string is going to contain the word "darwin", and so on. Detecting the OS is easy.

$uname = strtolower(php_uname());
if (strpos($uname, "darwin") !== false) {
    // It's OSX
} else if (strpos($uname, "win") !== false) {
    // It's windows
} else if (strpos($uname, "linux") !== false) {
    // It's Linux
} else {
    // It's something your script won't run on
}

这篇关于准确确定运行PHP的操作系统类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 03:11