What are some best practices for setting file permissions in PHP scripts to avoid permission denied errors?

When working with file permissions in PHP scripts, it is important to ensure that the files and directories have the correct permissions set to avoid permission denied errors. One common best practice is to set the permissions of files to 644 and directories to 755. This allows the owner to read and write files, while also allowing others to read files and access directories.

// Set file permissions to 644 and directory permissions to 755
chmod("path/to/file.txt", 0644);
chmod("path/to/directory", 0755);