How can file upload errors be effectively debugged in PHP scripts?

File upload errors in PHP scripts can be effectively debugged by checking the $_FILES array for error codes after a file upload attempt. Common error codes include UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, and UPLOAD_ERR_CANT_WRITE. By identifying the specific error code, developers can troubleshoot and resolve the issue accordingly.

if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    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;
        default:
            echo 'Unknown upload error';
            break;
    }
}