Wikis & learning

Distinguish an absent upload from a completed one

An upload handler receives a status code before it decides whether a temporary file is usable. A missing file, a successful transfer and a transfer error are different states.

An upload handler receives a status code before it decides whether a temporary file is usable. A missing file, a successful transfer and a transfer error are different states.

Walkthrough

Represent those states as separate branches. A successful transfer still needs independent checks on the file size, decoded content and destination before storage.

PHP example

<?php
function uploadState(array $file): string {
    return match ($file['error'] ?? UPLOAD_ERR_NO_FILE) {
        UPLOAD_ERR_NO_FILE => 'No file selected',
        UPLOAD_ERR_OK => 'Ready for validation',
        default => 'Transfer incomplete'
    };
}
echo uploadState([]), PHP_EOL;
echo uploadState(['error' => UPLOAD_ERR_OK]);

Expected output

No file selected
Ready for validation

Change and check

Add the UPLOAD_ERR_PARTIAL case. Decide which user-facing action makes sense for that outcome and check it with a sample record.