What are the potential security risks of setting directory permissions to 777 in a PHP script?

Setting directory permissions to 777 in a PHP script can pose significant security risks as it allows anyone to read, write, and execute files within that directory. This can potentially lead to unauthorized access, data breaches, and malicious code execution. To mitigate this risk, it is recommended to set more restrictive permissions, such as 755, which allows the owner to read, write, and execute files, while others can only read and execute.

// Set directory permissions to 755
$directory = '/path/to/directory';
chmod($directory, 0755);