Are there any best practices for handling file uploads in PHP, especially when dealing with different PHP versions?

When handling file uploads in PHP, it is important to consider the different PHP versions and their various configurations that may affect the file upload process. One best practice is to check for errors and validate the uploaded file before moving it to the final destination. Additionally, it is recommended to set appropriate upload limits and ensure proper file permissions are set for the upload directory.

// Check for file upload errors
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    die("File upload failed with error code: " . $_FILES['file']['error']);
}

// Validate the file type and size
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
$maxSize = 5 * 1024 * 1024; // 5MB
if (!in_array($_FILES['file']['type'], $allowedTypes) || $_FILES['file']['size'] > $maxSize) {
    die("Invalid file type or size. Allowed types: " . implode(', ', $allowedTypes) . ". Max size: 5MB.");
}

// Move the uploaded file to the upload directory
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
    echo "File uploaded successfully.";
} else {
    echo "Failed to move file to destination directory.";
}