What are best practices for handling file uploads in PHP to avoid permission denied errors?

When handling file uploads in PHP, it's important to ensure that the destination directory has the correct permissions set to allow the PHP script to write to it. To avoid permission denied errors, you can use the `chmod` function in PHP to set the appropriate permissions on the upload directory before moving the uploaded file.

$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);

// Set the correct permissions on the upload directory
if (!file_exists($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

// Move the uploaded file to the destination directory
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
    // File uploaded successfully
} else {
    // Handle upload error
}