Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 23, 2025

This PR fixes a TypeError that occurs during file upload when the uploader encounters array values in POST data from multi-select form fields.

Issue

The error mb_strlen(): Argument #1 ($string) must be of type string, array given was being thrown at line 211 in uploader_iw_upload_handler.php. This happened when metadata fields contained array values (e.g., from checkboxes or multi-select inputs) instead of simple strings.

// Original problematic code
if(isset($this->savedPostVars[$metaName]) && mb_strlen($this->savedPostVars[$metaName]) > 0) {
    $value = $this->savedPostVars[$metaName];
}

Solution

Added proper type checking to handle both string and array values gracefully:

// Fixed code with type checking
if(isset($this->savedPostVars[$metaName])) {
    $postValue = $this->savedPostVars[$metaName];
    // Handle both string and array values
    if (is_string($postValue) && mb_strlen($postValue) > 0) {
        //Uploader-Feature: Nutze angegebene Daten für alle Dateien
        $value = $postValue;
    } elseif (is_array($postValue) && !empty($postValue)) {
        //Handle array values (e.g., from multi-select fields)
        $value = implode(',', array_filter($postValue));
    }
}

Changes

  • String values: Used directly if not empty (preserves existing behavior)
  • Array values: Converted to comma-separated string using implode() and array_filter()
  • Other types: Skipped, keeping the default value from the media file

Benefits

  • ✅ Fixes the TypeError that prevented successful uploads
  • ✅ Maintains backward compatibility for existing string-based metadata
  • ✅ Adds support for multi-select form fields in metadata
  • ✅ Minimal, surgical change with no breaking modifications

Files are now uploaded successfully without the "unknown error" message, while still properly handling metadata from various form field types.

Fixes #96.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: skerbis <791247+skerbis@users.noreply.github.com>
Copilot AI changed the title [WIP] Fehler beim Upload Fix mb_strlen() TypeError when handling array values in POST data Jul 23, 2025
Copilot AI requested a review from skerbis July 23, 2025 09:28
@skerbis skerbis requested a review from olien July 23, 2025 09:29
@skerbis skerbis removed their assignment Jul 23, 2025
@olien
Copy link
Member

olien commented Jul 31, 2025

Habs getestet. Funktioniert. Top! Danke!

@skerbis skerbis removed request for olien and skerbis July 31, 2025 09:35
@skerbis skerbis marked this pull request as ready for review July 31, 2025 09:35
Copilot AI review requested due to automatic review settings July 31, 2025 09:35
@skerbis skerbis merged commit 4ea1d77 into main Jul 31, 2025
@skerbis skerbis deleted the copilot/fix-96 branch July 31, 2025 09:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a TypeError that occurs when the file uploader encounters array values in POST data from multi-select form fields, specifically addressing the mb_strlen(): Argument #1 ($string) must be of type string, array given error.

  • Adds type checking to handle both string and array values in POST data
  • Converts array values to comma-separated strings using implode()
  • Maintains backward compatibility for existing string-based metadata handling

$value = $postValue;
} elseif (is_array($postValue) && !empty($postValue)) {
//Handle array values (e.g., from multi-select fields)
$value = implode(',', array_filter($postValue));
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using array_filter() without a callback will remove falsy values (0, '', false, null). This might unintentionally remove valid form values like '0' from checkboxes or selects. Consider using array_filter($postValue, 'strlen') to only remove empty strings while preserving other falsy but valid values.

Suggested change
$value = implode(',', array_filter($postValue));
$value = implode(',', array_filter($postValue, 'strlen'));

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fehler beim Upload

3 participants