使路径在 linux 和 Windows 上都有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4178263/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:57:16  来源:igfitidea点击:

Make a path work both on linux and Windows

phpwindowslinux

提问by sanders

How can I make sure that this path:

我如何确保这条路径:

new Zend_Log_Writer_Stream(APPLICATION_PATH . '\logs\app.log')  

works both on linux and on windows?

在 linux 和 windows 上都可以使用吗?

采纳答案by Ben Lee

In Linux, the path separator is /. In Windows, it is either \or /. So just use forward slashes and you will be fine.

在 Linux 中,路径分隔符是/. 在 Windows 中,它是\/。所以只要使用正斜杠就可以了。

APPLICATION_PATH . '/logs/app.log'

回答by Qwerty

You can also use DIRECTORY_SEPARATORconstant instead of \or /. Usually you'll want to redefine it to have shorter name, like

您也可以使用DIRECTORY_SEPARATOR常量代替\/。通常你会想要重新定义它以获得更短的名称,比如

define('DS', DIRECTORY_SEPARATOR);
$filename = APP . DS . 'logs' . DS . 'file.txt';

回答by Edwin Irausquin

if you want to communicate two or more app of your site, this trick will serve you much

如果你想交流网站的两个或更多应用程序,这个技巧会对你很有帮助

$ Document_root = realpath ( \ filter_input ( INPUT_SERVER , ' DOCUMENT_ROOT '));

this is to convert the route you back real path and then just have to navigate between directories with the DIRECTORY_SEPARATOR without worrying about the operating system installed on your machine or web server

这是为了转换您返回真实路径的路线,然后只需使用 DIRECTORY_SEPARATOR 在目录之间导航,而不必担心安装在您的机器或 Web 服务器上的操作系统

回答by user2928048

Just realpath() is seems to be enough

只是 realpath() 似乎就足够了

Example #2

示例#2