定義済み定数
-
DIRECTORY_SEPARATOR
(string) -
PATH_SEPARATOR
(string) - Windows の場合はセミコロン、それ以外の場合はコロンとなります。
-
SCANDIR_SORT_ASCENDING
(int) -
SCANDIR_SORT_DESCENDING
(int) -
SCANDIR_SORT_NONE
(int)
+add a note
User Contributed Notes 2 notes
Anonymous ¶
10 years ago
In PHP 5.6 you can make a variadic function.
<?php
/**
* Builds a file path with the appropriate directory separator.
* @param string $segments,... unlimited number of path segments
* @return string Path
*/
function file_build_path(...$segments) {
return join(DIRECTORY_SEPARATOR, $segments);
}
file_build_path("home", "alice", "Documents", "example.txt");
?>
In earlier PHP versions you can use func_get_args.
<?php
function file_build_path() {
return join(DIRECTORY_SEPARATOR, func_get_args($segments));
}
file_build_path("home", "alice", "Documents", "example.txt");
?>
Anonymous ¶
11 years ago
For my part I'll continue to use this constant because it seems more future safe and flexible, even if Windows installations currently convert the paths magically. Not that syntax aesthetics matter but I think it can be made to look attractive:
<?php
$path = join(DIRECTORY_SEPARATOR, array('root', 'lib', 'file.php');
?>
↑ and ↓ to navigate •
Enter to select •
Esc to close
Press Enter without
selection to search using Google