What are the best practices for handling directory paths in PHP to avoid overwriting?

When handling directory paths in PHP, it is important to ensure that you are not overwriting existing files or directories unintentionally. One way to avoid this issue is to always check if the directory or file already exists before creating or moving it. Additionally, using functions like `mkdir()` with the appropriate flags can help prevent overwriting existing directories.

$directory = '/path/to/directory';

if (!file_exists($directory)) {
    mkdir($directory, 0777, true);
} else {
    // Handle existing directory
}