What is the purpose of setting file permissions using chmod in PHP?

Setting file permissions using chmod in PHP allows you to control who can read, write, and execute files on your server. This is important for security reasons, as it helps prevent unauthorized access to sensitive files and directories. By using chmod, you can specify the level of access for different users (owner, group, others) on a file or directory.

// Set file permissions to allow only owner to read and write
$file = 'example.txt';
$permissions = 0600; // Owner can read and write
chmod($file, $permissions);