为 *Nix 和 Windows 设置 PHP 包含路径的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1959772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to set PHP include path for *Nix and Windows
提问by Jeff
Is this the proper way to define
an include path for both *nix and Windows?
这是define
包含*nix 和 Windows路径的正确方法吗?
define( 'INCPATH', realpath( dirname( __FILE__ ) ) . '/' );
Note the trailing forward-slash I included above. Is the forward-slash for includes/requires the same for both OS's, as well?
请注意我上面包含的尾随正斜杠。两个操作系统的包含/要求的正斜杠是否也相同?
EDIT (UPDATED WITH ANSWER):
编辑(更新答案):
From what I can gather, my code below is the proper way to universally define an include/require path for both *nix and Windows OS's. Feel free to correct anything in the comments below.
据我所知,我下面的代码是为 *nix 和 Windows 操作系统普遍定义包含/需要路径的正确方法。请随时更正下面评论中的任何内容。
The thing that confused me were the many examples I saw showing replacement of back-slashes (\)
into forward-slashes(/)
. Based on some of the answers below, this is unnecessary.
令我困惑的是我看到的许多示例显示替换back-slashes (\)
into forward-slashes(/)
。根据下面的一些答案,这是不必要的。
So the final correct code (for the purist) is:
所以最终正确的代码(对于纯粹主义者)是:
define( 'INCPATH', realpath( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR );
That code produces the following results:
该代码产生以下结果:
*nix: /path/to/the/file/
*nix: /path/to/the/file/
Windows: C:\Path To\the\file\
Windows: C:\Path To\the\file\
A brief explanation, working our way from the inside (__FILE__
) out (realpath()
):
一个简单的解释,从内(__FILE__
)到(realpath()
):
FILEThe full path and filename of the file. Always contains an absolute path with symlinks resolved.
FILE 文件的完整路径和文件名。始终包含已解析符号链接的绝对路径。
dirname()The returned string is pathwith any trailing /component removed. Responsible for removing the filename.
dirname()返回的字符串是路径,删除了任何尾随的 /component。负责删除文件名。
realpath()Returns the canonicalized (normalized/standardized) absolute pathname on success. The resulting path will have no symbolic link, '/./'
or '/../'
components. I assume this is included for thoroughness because __FILE__
already resolves symlinks. Or maybe it's included to resolve relative paths? Either way, it seems to solidify our goal.
realpath()在成功时返回规范化(规范化/标准化)绝对路径名。生成的路径将没有符号链接'/./'
或'/../'
组件。我认为这是为了彻底而包括在内,因为__FILE__
已经解决了符号链接。或者它可能包含在解析相对路径中?无论哪种方式,它似乎都巩固了我们的目标。
回答by Derek Illchuk
Forward slashes will work for both OS's, and it's the way to go.
正斜杠适用于两个操作系统,这是要走的路。
I couldn't find an absolute reference to this, but it's indicated in several places in the PHP manual, like hereand here. And, it works for me, a Windows & Linux user.
我找不到对此的绝对引用,但在 PHP 手册中的几个地方都指出了它,例如这里和这里。而且,它适用于我,一个 Windows 和 Linux 用户。
Lastly, you may end up specifying mixed-paths on Windows, like c:\\apache\\htdocs\\myapp/index.php
, and that all works fine.
最后,您可能最终会在 Windows 上指定混合路径,例如c:\\apache\\htdocs\\myapp/index.php
,并且一切正常。
回答by Decent Dabbler
Alternatively you can use PHP's predefined constant DIRECTORY_SEPARATOR
, which will give you the OS-specific directory delimiter. See http://www.php.net/manual/en/dir.constants.phpalso.
或者,您可以使用 PHP 的预定义常量DIRECTORY_SEPARATOR
,它将为您提供特定于操作系统的目录分隔符。另请参阅http://www.php.net/manual/en/dir.constants.php。
回答by wallyk
To many people's surprise, /
works fine on Windows—and MSDOS. Within pathnames, it works even on OpenVMS.
令许多人惊讶的是,它/
在 Windows 和 MSDOS 上运行良好。在路径名中,它甚至可以在 OpenVMS 上工作。
However, if you are doing something within PHP for paths, an array would be a more convenient structure than a string.
但是,如果您在 PHP 中为路径做一些事情,那么数组将是比字符串更方便的结构。
$MYPATH = array ('.', '/usr/lib/', '/usr/share/lib');