exists()

public function exists(string $path): bool

Description

Checks whether a file or directory exists at the specified path.

Parameters

ParameterTypeDescription
$path string The path to the file or directory.

Return Value

TypeDescription
bool Returns true if the file or directory exists, otherwise false.

Example #1

The code sample below demonstrates how to check if a user's profile picture exists before displaying it.

Example #2

The example above shows how to verify a directory exists before attempting to list its contents.

Example #3

The example above demonstrates how to safely clean up temporary files only if they exist.

Checking Files vs Directories: The exists() method returns true for both files and directories. If you need to specifically verify that a path is a directory (not a file), use PHP's native is_dir($path) function instead. Similarly, use is_file($path) to confirm a path is specifically a file.