What potential security risks are associated with setting file permissions to 777 in PHP?

Setting file permissions to 777 in PHP can pose a significant security risk as it grants read, write, and execute permissions to everyone, including unauthorized users. This can potentially allow malicious actors to modify or delete critical files, leading to data breaches or system compromise. To mitigate this risk, it is recommended to set more restrictive file permissions based on the principle of least privilege.

// Set file permissions to 644 (read/write for owner, read for group and others)
$file = 'example.txt';
chmod($file, 0644);