What are the best practices for handling file uploads in PHP scripts to avoid issues with undefined variables and errors?

When handling file uploads in PHP scripts, it is important to check if the file upload was successful and handle any potential errors that may arise, such as undefined variables. To avoid these issues, always check if the file was uploaded using the `$_FILES` superglobal and verify that there are no errors before proceeding with any file processing operations.

if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){
    // File upload was successful, proceed with processing
    $file_name = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    
    // Additional file processing code here
} else {
    // Handle file upload error
    echo "Error uploading file.";
}