Php - How to create a path in a portable way using the PHP API

Problem:

Both, dirname()and realpath()return the path with the correct slash, depending on which OS you are running the script on.

I want to create a path in a portable way (using the PHP API), so I don't need to worry about slashes \or /.

realpath()returns FALSEif the path does not exist. So this is not an option:

define("MY_PATH", realpath(__DIR__."/myNewDirToCreate");

Otherwise, dirname()it will not be used for:

define("MY_FILE", "I/should/use/an/abstract/path/definition.file");

because it will return the parten directory, not the file path. I could solve the problem myself using either regexor stringreplace. But it PHPis a cross-platform environment, and of course, I skip some functions in the extensive PHP documentation.

Question:

  • , , script , PHP API, ? path("no/matter/what/os/are/you/running/this.file\or\what\slashes\you\are\using");
+1
1

, DIRECTORY_SEPARATOR.

$path_to_file = $directory . DIRECTORY_SEPARATOR . $file

: http://php.net/manual/en/dir.constants.php#constant.directory-separator

, PHP , Windows , PHP!

+4

All Articles