PHP message: PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string

PHP message: PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string

You are encountering a PHP Fatal error in your PHP 8 environment because you are attempting to access an array offset using a string key on a variable that is actually a scalar value (a string). The specific line triggering the crash is $uploadZoneData[1]['size']. In PHP 7.4, this code would have executed without issue, but PHP 8 introduced stricter type checking for mixed arrays to prevent subtle bugs from silently corrupting data structures.

The root cause lies in the return value of your uploadSingleFile function. You are returning an array initialized as $status, $newfilename, and $errors. However, the logic inside updateZoneData assumes that $uploadZoneData[1] is always an associative array containing a 'size' key.

When the upload fails (the else block where $status is set to '0'), your function returns an array where the second element ($uploadZoneData[1]) corresponds to $newfilename. Since $newfilename is constructed using string concatenation, it is a plain string. When you try to execute $uploadZoneData[1]['size'], PHP 8 correctly identifies that you are trying to access an array key on a string and throws the TypeError.

To fix this, you must ensure that the data structure returned by uploadSingleFile is consistent regardless of success or failure. You should not rely on mixing scalar values and associative arrays in the same indexed list without explicit documentation or type safety.

Standardizing the Return Structure

The most robust solution is to refactor $uploadSingleFile to always return a predictable array structure, such as ['status' => int, 'filename' => string|null, 'errors' => array]. This allows the calling function to check the status first before attempting to access file metadata.

Here is how you can modify the uploadSingleFile function:

function uploadSingleFile($zoneImage, $fileMoveTo, $fileAllowedExtensions, $fileAllowedSize, $zoneCustomer, $zone)
{
    global $config;

    // define file upload settings
    $errors = [];
    $fileName = $zoneImage['name'];
    $fileSize = $zoneImage['size'];
    $fileTmp = $zoneImage['tmp_name'];
    $fileType = $zoneImage['type'];
    $fileExt = strtolower(end(explode('.', $zoneImage['name'])));
    $MBtoByte = $fileAllowedSize * 1048576;
    $extensions = $fileAllowedExtensions;

    // define errors
    if (in_array($fileExt, $extensions) === false) {
        $errors[] = "Dieser Datei-Typ ist nicht erlaubt";
    }

    if ($fileSize > $MBtoByte) {
        $errors[] = 'Die Datei ist zu gross';
    }

    // finally try to upload the file
    if (empty($errors) == true) {
        $temp = explode(".", $zoneImage["name"]);
        $newfilename = $zoneCustomer . '-' . strtoupper($zone) . '-' . uniqid() . '.' . end($temp);

        move_uploaded_file($fileTmp, $_SERVER["DOCUMENT_ROOT"] . $fileMoveTo . $newfilename);
        
        // Return success status with the new filename as a scalar or array depending on need
        // Let's return a consistent structure: ['status' => 1, 'filename' => $newfilename, 'errors' => []]
        return [
            'status' => '1', 
            'filename' => $newfilename, 
            'errors' => []
        ];
    } else {
        // Return failure status with the filename (original or empty) and collected errors
        // Crucially, we maintain the structure so index 1 is always accessible as an array if needed, 
        // OR we change the access pattern in updateZoneData.
        
        // Option A: Keep scalar return but enforce type safety in caller
        // Let's stick to a consistent associative array for clarity.
        return [
            'status' => '0', 
            'filename' => null, // or $newfilename if you want to capture the attempted name
            'errors' => $errors
        ];
    }
}

However, changing the function signature requires updating every caller. A less invasive fix that respects the existing code flow while satisfying PHP 8 is to ensure that $uploadZoneData[1] is always an array, or to change how updateZoneData accesses the data.

Adjusting the Caller Logic

If you prefer not to refactor the entire return structure immediately, you can adjust the logic in updateZoneData to check the type before accessing offsets. This approach prevents the fatal error by ensuring you only treat $uploadZoneData[1] as an array when it contains file metadata.

if ($uploadZoneData[0] == '1') {
    // On success, $uploadZoneData[1] is a string filename in your current logic.
    // You likely don't need to check the size here if the upload succeeded.
    
    // If you absolutely need the size from the return value, ensure it's returned as an array:
    // return ['status' => '1', 'size' => $fileSize, ...];
}

if ($uploadZoneData[0] == '0') {
    // On failure, $uploadZoneData[1] is a string.
    // Do not try to access ['size'] here if it's not guaranteed to be an array.
    // Instead, check the errors in $uploadZoneData[2].
    
    $collectedErrors = array_flatten($uploadZoneData[2]);
    // ... handle errors
}

The core lesson from this migration is that PHP 8 treats mixed arrays as a design flaw rather than an edge case. If your code relies on the fact that $uploadZoneData[1] might be a string in one branch and an array in another, you must explicitly handle that polymorphism or normalize the data structure before use.

For large-scale applications running on PHP 8+, consider adopting type declarations for function arguments and return types. If uploadSingleFile is intended to return file metadata, it should declare array as its return type hint. This forces you to maintain a consistent array structure, eliminating the ambiguity that caused the TypeError.

If you are managing a legacy codebase like this, the most pragmatic step is often to normalize the data immediately upon receipt in the calling function before processing it further, ensuring that all downstream logic receives uniform types.

Our network