realpath
Returns the full path without `..`, `.` or symbolic links.
Syntax
realpath(string $path): string|falseParameters
| Parameter | Type | Description |
|---|---|---|
| $path | string | The path. |
Return value
string|falseThe full path, or `false` if the file does not exist.
Examples
A path with parent references:
<?php
$dir = sys_get_temp_dir();
var_dump(realpath($dir . '/../' . basename($dir)) === realpath($dir));
var_dump(realpath($dir . '/no-such-file.txt'));Output:
bool(true)
bool(false)Notes
This is how you check that a requested file lies inside an allowed folder: the full path must start with that folder's path.
See also
- dirname — Returns the path of the folder that holds a file.
- basename — Returns the last part of a path — the file name.
- file_exists — Checks whether a file or folder exists.
In the tutorial
- Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.