What are the best practices for setting file permissions (CHMOD) in PHP for successful file uploads?

When setting file permissions (CHMOD) in PHP for successful file uploads, it is important to ensure that the directory where the files will be uploaded has the correct permissions set. The directory should typically have permissions set to 755 or 777 to allow for writing and reading files. Additionally, the uploaded files themselves should have permissions set to 644 to ensure they can be accessed and executed as needed.

// Set directory permissions for file uploads
$uploadDir = 'uploads/';
chmod($uploadDir, 0777);

// Set file permissions for uploaded files
$uploadedFile = $uploadDir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadedFile);
chmod($uploadedFile, 0644);