What best practices should be followed when setting permissions for directories created using PHP?

When setting permissions for directories created using PHP, it is important to follow best practices to ensure security and proper functionality. One common practice is to set the directory permissions to 755, which allows the owner to read, write, and execute, while others can only read and execute. This helps prevent unauthorized access to the directory.

$directory = 'path/to/directory';

// Set directory permissions to 755
if (!file_exists($directory)) {
    mkdir($directory, 0755, true);
}