What is the purpose of the realpath() function in PHP and how can it be used to clean up directory paths?

The realpath() function in PHP is used to get the canonicalized absolute pathname of a specified path. This function can be used to clean up directory paths by resolving any symbolic links, extra slashes, or relative paths to provide a normalized path that can be used consistently.

// Example usage of realpath() to clean up a directory path
$dirPath = '/var/www/html//project/../images/';
$cleanedPath = realpath($dirPath);

echo $cleanedPath;