w7corp/easywechat · error · RuntimeException

Failed to create temporary file.

Error message

Failed to create temporary file.

What it means

Kernel\Form\File::buildFileContent() guesses a MIME type by writing the contents to tempnam(sys_get_temp_dir(), 'easywechat') when no filename was provided; if tempnam() returns false it throws RuntimeException('Failed to create temporary file.'). tempnam fails when the system temp directory does not exist, is not writable by the PHP process, is excluded by open_basedir, or the disk is full.

Source

Thrown at src/Kernel/Form/File.php:54

    /**
     * @throws RuntimeException
     */
    public static function fromContents(
        string $contents,
        ?string $filename = null,
        ?string $contentType = null,
        ?string $encoding = null
    ): DataPart {
        if ($contentType === null) {
            $mimeTypes = new MimeTypes;

            if ($filename) {
                $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
                $contentType = $mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
            } else {
                $tmp = tempnam(sys_get_temp_dir(), 'easywechat');
                if (! $tmp) {
                    throw new RuntimeException('Failed to create temporary file.');
                }

                file_put_contents($tmp, $contents);
                $contentType = $mimeTypes->guessMimeType($tmp) ?? 'application/octet-stream';
                $filename = md5($contents).'.'.($mimeTypes->getExtensions($contentType)[0] ?? null);
            }
        }

        return new self($contents, $filename, $contentType, $encoding);
    }

    /**
     * @throws RuntimeException
     *
     * @deprecated since EasyWeChat 7.0, use fromContents() instead
     */
    public static function withContents(
        string $contents,

View on GitHub (pinned to f0cf0a8b83)

Solutions

  1. Pass an explicit $filename (and $contentType) to buildFileContent — when a filename is given the extension-based MIME lookup is used and tempnam is never called
  2. Ensure the temp dir is writable: check is_writable(sys_get_temp_dir()) and the open_basedir ini setting
  3. Point PHP at a writable directory via sys_temp_dir in php.ini if /tmp is unusable

Example fix

// before: no filename -> tempnam fallback
$part = File::buildFileContent($binary);

// after: filename given -> no temp file needed
$part = File::buildFileContent($binary, 'qrcode.png', 'image/png');
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight check before uploads without filenames
if (! is_writable(sys_get_temp_dir())) {
    throw new RuntimeException('System temp dir '.sys_get_temp_dir().' is not writable');
}
// Or avoid the temp path entirely by naming the file
$part = File::buildFileContent($binary, 'upload.png', 'image/png');

Prevention

When it happens

Trigger: Uploading in-memory content (e.g. ->post('media/upload', ['media' => DataPart::fromContents($binary)]) without a filename) on a server where /tmp is read-only, missing, full, or blocked by the open_basedir ini directive.

Common situations: Hardened shared hosting with restrictive open_basedir, containers with a read-only tmpfs, disk-full incidents, upload moving from dev (writable /tmp) to production.

Related errors


AI-assisted analysis of w7corp/easywechat@f0cf0a8b83 (2026-08-21). Data as JSON: /api/errors/cae3c681ca216eef. Report an issue: GitHub.