What are the potential security risks associated with allowing write permissions to directories in PHP scripts?

Allowing write permissions to directories in PHP scripts can pose a security risk as it opens the door to potential malicious activities such as unauthorized file uploads, overwriting critical files, and executing harmful scripts. To mitigate this risk, it is recommended to restrict write permissions to only necessary directories and properly validate and sanitize user input before writing to the filesystem.

// Example of restricting write permissions to a specific directory

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

if (is_writable($directory)) {
    // Proceed with writing to the directory
} else {
    echo "Directory is not writable.";
}