WordPress / WordPress/php-ai-client

File DTO: file_exists() called on oversized strings causes PHP warning spam when handling base64 image data

Open Beginner friendly
#258 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
308
Forks
84
Avg merge
7d 21h
Merged PRs (30d)
2

Description

Description

File::detectAndProcessFile() in src/Files/DTO/File.php calls file_exists() before checking whether the input string is plain base64 data. When a caller passes a large base64-encoded image (e.g. from Google Imagen / Gemini via bytesBase64Encoded), PHP emits:

PHP Warning: file_exists(): File name is longer than the maximum allowed path length on this platform (4096): /9j/4AAQSkZJRgABAQEBLAEsAAD/...

Because the base64 data is embedded verbatim in the warning message, each occurrence adds ~1 MB to the PHP error log. In production this caused a 5.3 MB error log from only 6–8 image generation calls.

Root Cause

The detection order in detectAndProcessFile() is:

  1. URL check → skip
  2. Data URI check → skip
  3. file_exists($file) called → PHP warning (string > 4096 chars)
  4. Plain base64 regex check → matches correctly

Base64-encoded JPEG data starts with /9j/ (the base64 representation of the JPEG magic bytes FF D8 FF), which looks like an absolute file path on Linux. PHP calls the filesystem with a ~750 KB–1 MB "path" and emits the warning before the code ever reaches the base64 branch.

Reproduction

// Simulate what ai-provider-for-google does when Google AI returns bytesBase64Encoded
$base64Jpeg = base64_encode(file_get_contents('/path/to/any/image.jpg')); // > 4 KB
$file = new \WordPress\AiClient\Files\DTO\File($base64Jpeg, 'image/jpeg');
// → PHP Warning: file_exists(): File name is longer than the maximum allowed path length on this platform (4096)

Affected code

File: src/Files/DTO/File.php, line 94 (as of the version bundled with WordPress 6.9)

// Before (bug):
if (file_exists($file) && is_file($file)) {

// After (fix):
if (strlen($file) <= PHP_MAXPATHLEN && file_exists($file) && is_file($file)) {

PHP_MAXPATHLEN is the PHP constant that reflects the OS limit (typically 4096 on Linux). A string longer than that can never be a valid file path, so short-circuiting the check is semantically correct and has no side effects on legitimate use cases.

Context

  • Observed on a WordPress 6.9 site using the ai-provider-for-google plugin v1.1.0
  • The plugin passes $predictionData['bytesBase64Encoded'] directly to new File(...), which is the documented usage (plain base64 with MIME type)
  • The File class is supposed to handle this case, but the file_exists() call causes a noisy warning before it gets there
  • Images returned by Google AI contain C2PA metadata, making them even larger and amplifying the log impact

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/Files/DTO/File.php around detectAndProcessFile() and its file_exists() call. Verify the oversized base64 input path and the existing plain-base64 handling, then prevent filesystem checks for strings longer than PHP_MAXPATHLEN. Done means large base64 images no longer emit the PHP warning while legitimate file paths and base64 data continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api, backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.