What is the significance of the 'error' key in the $_FILES array and how can it be leveraged for error handling?

The 'error' key in the $_FILES array holds the error code associated with the file upload. It can be leveraged for error handling to check if the file was uploaded successfully or if any errors occurred during the upload process. By checking the value of the 'error' key, you can determine the specific error that occurred and handle it accordingly in your code.

if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    // File uploaded successfully, proceed with processing
    $fileTmpPath = $_FILES['file']['tmp_name'];
    $fileName = $_FILES['file']['name'];
    // Additional processing code here
} else {
    // Handle file upload error
    switch ($_FILES['file']['error']) {
        case UPLOAD_ERR_INI_SIZE:
            echo 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
            break;
        case UPLOAD_ERR_FORM_SIZE:
            echo 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
            break;
        case UPLOAD_ERR_PARTIAL:
            echo 'The uploaded file was only partially uploaded';
            break;
        case UPLOAD_ERR_NO_FILE:
            echo 'No file was uploaded';
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            echo 'Missing a temporary folder';
            break;
        case UPLOAD_ERR_CANT_WRITE:
            echo 'Failed to write file to disk';
            break;
        case UPLOAD_ERR_EXTENSION:
            echo 'A PHP extension stopped the file upload';
            break;
        default:
            echo 'Unknown upload error';
            break;
    }
}