PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

File $path not found!

Error message

File $path not found!

What it means

For non-URL paths, setPath() (with the default $verifyFile = true) keeps the path only if @file_exists() passes and isImage() agrees; isImage() uses mime_content_type() (fileinfo extension) and requires a mime type starting with 'image/'. If the internal path stays empty the exception 'File ... not found!' is thrown — it covers missing files, unreadable paths, and files fileinfo cannot classify (including valid images when the fileinfo extension is not loaded).

Source

Thrown at src/PhpSpreadsheet/Worksheet/Drawing.php:176

                    $put = @file_put_contents($filePath, $imageContents);
                    if ($put !== false) {
                        if ($this->isImage($filePath)) {
                            $this->path = $path;
                            $this->setSizesAndType($filePath);
                        }
                        unlink($filePath);
                    }
                }
            }
        } else {
            $exists = @file_exists($path);
            if ($exists !== false && $this->isImage($path)) {
                $this->path = $path;
                $this->setSizesAndType($path);
            }
        }
        if ($this->path === '' && $verifyFile) {
            throw new PhpSpreadsheetException("File $path not found!");
        }

        if ($this->worksheet !== null) {
            if ($this->path !== '') {
                $this->worksheet->getCell($this->coordinates);
            }
        }

        return $this;
    }

    private function isImage(string $path): bool
    {
        $mime = (string) @mime_content_type($path);
        $retVal = false;
        if (str_starts_with($mime, 'image/')) {
            $retVal = true;
        } elseif ($mime === 'application/octet-stream') {

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Pre-check with an absolute path: $path = realpath($relative); is_file($path) && str_starts_with((string) mime_content_type($path), 'image/').
  2. If fileinfo is missing, install/enable the extension (e.g. install-ext fileinfo in your Docker image) — isImage() depends on it.
  3. Attach the drawing before the file is moved or unlinked; regenerate corrupted sources.

Example fix

// before
$drawing->setPath('uploads/logo.png'); // File uploads/logo.png not found!

// after
$path = realpath('uploads/logo.png');
if ($path === false || !str_starts_with((string) @mime_content_type($path), 'image/')) {
    throw new RuntimeException('drawing source missing or not recognized as an image');
}
$drawing->setPath($path);
Defensive patterns

Strategy: validation

Validate before calling

$path = realpath($relativePath);
if ($path === false || !is_file($path) || !str_starts_with((string) @mime_content_type($path), 'image/')) {
    throw new InvalidArgumentException('drawing source missing or not recognized as an image');
}
$drawing->setPath($path);

Type guard

function isUsableDrawingFile(string $path): bool
{
    return is_file($path)
        && str_starts_with((string) @mime_content_type($path), 'image/');
}

Try / catch

try {
    $drawing->setPath($path);
} catch (PhpSpreadsheetException $e) {
    // regenerate or re-upload the source file, then retry
}

Prevention

When it happens

Trigger: $drawing->setPath('/tmp/upload42.png') after the temp file was moved or deleted; a relative path resolved against a different cwd (web vs CLI); open_basedir restrictions making file_exists() fail silently; a PHP build without fileinfo so mime_content_type() returns false for every file.

Common situations: Attaching drawings after moving/unlinking uploads; Docker or alpine php-cli images missing ext-fileinfo; truncated or non-image uploads; passing a directory or a dangling symlink.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/7c371ad2a4911b1f. Report an issue: GitHub.