What are some security considerations to keep in mind when working with directory paths in PHP?
When working with directory paths in PHP, it is important to sanitize user input to prevent directory traversal attacks. One way to do this is by using the realpath() function to get the canonicalized absolute pathname of a given path. This function resolves any symbolic links and references to parent directories, ensuring that the path is safe to use.
$directory = '/path/to/directory';
$absolute_path = realpath($directory);
if ($absolute_path !== false) {
// Use $absolute_path safely
} else {
// Handle invalid directory path
}