How can file permissions be managed effectively during file uploads in PHP?

When uploading files in PHP, it's important to manage file permissions effectively to ensure security and control access to the uploaded files. This can be achieved by setting the appropriate permissions on the uploaded file using PHP's chmod function after the file has been successfully uploaded.

// Upload file
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["file"]["name"]);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
    // Set file permissions
    chmod($targetFile, 0644);
    echo "File uploaded successfully.";
} else {
    echo "Error uploading file.";
}