Are there any best practices for handling input type file values in PHP to avoid security vulnerabilities?

When handling input type file values in PHP, it is important to validate and sanitize the file before processing it to avoid security vulnerabilities such as file upload attacks or arbitrary code execution. One best practice is to check the file type and size before moving the file to a secure location on the server.

// Example code snippet to handle input type file values securely in PHP

if(isset($_FILES['file'])){
    $file = $_FILES['file'];

    // Check file type
    $allowed_types = ['image/jpeg', 'image/png'];
    if(!in_array($file['type'], $allowed_types)){
        die("Invalid file type. Only JPEG and PNG files are allowed.");
    }

    // Check file size
    $max_size = 1048576; // 1MB
    if($file['size'] > $max_size){
        die("File is too large. Maximum file size allowed is 1MB.");
    }

    // Move file to secure location
    $upload_dir = 'uploads/';
    $upload_file = $upload_dir . basename($file['name']);
    if(move_uploaded_file($file['tmp_name'], $upload_file)){
        echo "File uploaded successfully.";
    } else {
        echo "Error uploading file.";
    }
}