What are the best practices for setting file permissions when working with PHP upload scripts?

When working with PHP upload scripts, it is important to set appropriate file permissions to ensure the security of your server. A common best practice is to set the permissions of uploaded files to 0644 and directories to 0755. This allows the web server to read and execute the files, while preventing unauthorized write access.

// Set file permissions for uploaded files
$uploaded_file = '/path/to/uploaded/file.jpg';
chmod($uploaded_file, 0644);

// Set file permissions for uploaded directories
$uploaded_dir = '/path/to/uploaded/directory/';
chmod($uploaded_dir, 0755);