开发者

What Path is acceptable?

开发者 https://www.devze.com 2022-12-20 13:43 出处:网络
I am 开发者_StackOverflow中文版using below statement to return the directory name of the running script:

I am 开发者_StackOverflow中文版using below statement to return the directory name of the running script:

print dirname(__FILE__);

it outputs something like this with back-slashes:

www\EZPHP\core\ezphp.php

Question:

Is a path with back-slashes acceptable across all major operating systems? If not, how should i construct the path either with slashes or back-slashes so that it is acceptable on all major operating systems eg Windows, Linux, Ubuntu.

Thank You.


Forward slashes are a good route.

There is also a constant called DIRECTORY_SEPARATOR that will return the directory separator for the system the code is running on.

I use forward slashes when I write paths for all my apps, and I often use DIRECTORY_SEPARATOR when I am exploding the results of a call that returns a path so that I can ensure I always have the right one to break on.

HTH, Jc


I would normalize that to forward slashes. Windows accepts forward slashes, and they are the default on *nix systems

print str_replace('\\','/',dirname(__FILE__));


In reality, it doesn't matter... this is because dirname() doesn't necessarily return backslashes: it returns whatever directory separator is used by the OS. That is to say, whatever dirname returns is the separator you should be using anyway.

Other than that, just use forward slashes: PHP will interpret it correctly in Windows and Linux.


It doesn't matter, dirname() always return the path in the OS format.

dirname('c:/x'); // returns 'c:\'
dirname('c:/Temp/x'); // returns 'c:/Temp'
dirname('/x'); // returns '\'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号