What are the best practices for handling file permissions in PHP to ensure secure access to files on a server?

To ensure secure access to files on a server in PHP, it is important to properly set file permissions. This involves assigning the appropriate read, write, and execute permissions to files and directories based on the principle of least privilege. It is recommended to restrict file permissions to only the necessary users or groups to prevent unauthorized access or modification of files.

// Set file permissions to 0644 for files and 0755 for directories
chmod('/path/to/file', 0644);
chmod('/path/to/directory', 0755);