Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

realpath

Returns the full path without `..`, `.` or symbolic links.

Syntax

realpath(string $path): string|false

Parameters

ParameterTypeDescription
$pathstringThe 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.

Thanks for reading!

Donate to the site — we work hard for you.