开发者

Using DIRECTORY_SEPARATOR in file_exists() on Windows

开发者 https://www.devze.com 2023-01-12 16:18 出处:网络
var_dump(DIRECTORY_SEPARATOR) // string \'\\\' (length=1) var_dump(file_exists(\"C:/1212.txt\")); // true
var_dump(DIRECTORY_SEPARATOR) // string '\' (length=1)
var_dump(file_exists("C:/1212.txt")); // true
var_dump(file_exists("C:\1212.txt")); // false
var_dump(file_exists("C:".DIRECTORY_SEPARATOR."1212.txt")); /开发者_如何转开发/ false

What does DIRECTORY_SEPARATOR do? Why is the above case false when using DIRECTORY_SEPARATOR?


What DIRECTORY_SEPARATOR do?

DIRECTORY_SEPARATOR equals to "/" (Unix) or "\" (Windows) depending on the platform.

Why above case is false with DIRECTORY_SEPARATOR?

Because in double-quoted strings "\123" translates to "Q" (more details in PHP Manual).

When construction Windows paths, you should escape backslash: "C:\\1212.txt" or use single-quoted strings: 'C:\1212.txt'.

Even better and cleaner way would be to use Unix directory separator "/" hard-coded directly in path string (without any constants), it works just fine under Windows: "C:/1212.txt".


When nit comes to PS - Path Separators here's a tip for you:

Linux supports / only

Windows support \ and /

so my advice would be to make everything /

define('DS','/'); //Should work.

If your building your application to be cross platformt hen think about doing this.

define('DS','/');
define('BASE_ROOT',str_replace('\\',DS,dirname(__FILE__)));
require_once BASE_ROOT . DS . 'includes' . DS . 'init.php';

Then it should work nice on both platforms.

Learn:

http://en.wikipedia.org/wiki/Filename

http://en.wikipedia.org/wiki/Path_(computing)


try:

var_dump(file_exists("C:\\1212.txt"));

The backslash is the escape character to to add one to a string you need to follow it with another.


DIRECTORY_SEPARATOR returns / on Unix systems and \ on Win.


DIRECTORY_SEPARATOR is a PHP constant, helping programmers to write script that works across different operating systems.

Assuming you're on a Windows machine, it looks like yours is misconfigured.


I have seen some mistakes on blogs, and other places concerning Windows being "just fine" with forward slashes, and there isn't a need for *Directory Separator constants which provide \ or / characters depending on if you are running Windows, Nix, etc ...

These constants are /very/ necessary because forward slash support in Windows is only /partially/ implemented.

Most Windows Command Line Utilities have switches that are accessed using /, forward slash, rather than hyphens, -, which is unix style. This is why the directory separator constants are important in scripts.

Broken Example:

C:\Temp>del C:/Temp/*.o /S /Q

Error Message: Invalid switch - "Temp".

C:\Temp>


DIRECTORY_SEPARATOR is very unnecessary.

IMHO it should be dropped definitively.

There are no OS that uses other than '/', I mean OS on which you would run a webserver (MacOs classic? seriously?)

To comment "Wind And Flame" reply, his example is broken too, because if you decide to execute system commands, then you loose portability in any case. Of course system command are expressed in system syntax. But Every PHP command that wrap system calls, such as fopen, file_get_content etc work fine with '/' on windows.

0

精彩评论

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