Constantes predefinidas

Estas constantes están definidas por esta extensión y estarán disponibles sólo cuando la extensión haya sido compilada con PHP, o bien sea cargada dinámicamente en ejecución.

DIRECTORY_SEPARATOR (string)
PATH_SEPARATOR (string)
Disponible desde PHP 4.3.0. Punto y coma en Windows, en caso contrario dos puntos.
SCANDIR_SORT_ASCENDING (integer)
Disponible desde PHP 5.4.0.
SCANDIR_SORT_DESCENDING (integer)
Disponible desde PHP 5.4.0.
SCANDIR_SORT_NONE (integer)
Disponible desde PHP 5.4.0.
add a note add a note

User Contributed Notes 4 notes

up
14
Anonymous
1 year 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");
?>
up
14
Anonymous
1 year 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');
?>
up
-1
Harrison
1 month ago
DIRECTORY_SEPARATOR is a lifesaver! A hacker must never know what php files you have included into your php webpages or your php scripts that javascript calls upon. If you were to use '/' to include a file, the hacker can instantly know that the file you included is there, however, if you use DIRECTORY_SEPARATOR then the hacker would not know the file has been included.
up
-7
Paulo Marques
1 year ago
DIRECTORY_SEPARATOR is not necessarily needed, PHP always converts / to the appropriate character in its file functions.
It is good practice, though.
To Top