{"record":{"id":"cae3c681ca216eef","repo":"w7corp/easywechat","slug":"failed-to-create-temporary-file","errorCode":null,"errorMessage":"Failed to create temporary file.","messagePattern":"Failed to create temporary file\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Kernel/Form/File.php","lineNumber":54,"sourceCode":"    /**\n     * @throws RuntimeException\n     */\n    public static function fromContents(\n        string $contents,\n        ?string $filename = null,\n        ?string $contentType = null,\n        ?string $encoding = null\n    ): DataPart {\n        if ($contentType === null) {\n            $mimeTypes = new MimeTypes;\n\n            if ($filename) {\n                $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));\n                $contentType = $mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';\n            } else {\n                $tmp = tempnam(sys_get_temp_dir(), 'easywechat');\n                if (! $tmp) {\n                    throw new RuntimeException('Failed to create temporary file.');\n                }\n\n                file_put_contents($tmp, $contents);\n                $contentType = $mimeTypes->guessMimeType($tmp) ?? 'application/octet-stream';\n                $filename = md5($contents).'.'.($mimeTypes->getExtensions($contentType)[0] ?? null);\n            }\n        }\n\n        return new self($contents, $filename, $contentType, $encoding);\n    }\n\n    /**\n     * @throws RuntimeException\n     *\n     * @deprecated since EasyWeChat 7.0, use fromContents() instead\n     */\n    public static function withContents(\n        string $contents,","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/w7corp/easywechat/blob/f0cf0a8b8361417ed683b8246d0ecbaf0aafcaa8/src/Kernel/Form/File.php#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hardened shared hosting with restrictive open_basedir, containers with a read-only tmpfs, disk-full incidents, upload moving from dev (writable /tmp) to production.","solutions":["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","Ensure the temp dir is writable: check is_writable(sys_get_temp_dir()) and the open_basedir ini setting","Point PHP at a writable directory via sys_temp_dir in php.ini if /tmp is unusable"],"exampleFix":"// before: no filename -> tempnam fallback\n$part = File::buildFileContent($binary);\n\n// after: filename given -> no temp file needed\n$part = File::buildFileContent($binary, 'qrcode.png', 'image/png');","handlingStrategy":"validation","validationCode":"// Pre-flight check before uploads without filenames\nif (! is_writable(sys_get_temp_dir())) {\n    throw new RuntimeException('System temp dir '.sys_get_temp_dir().' is not writable');\n}\n// Or avoid the temp path entirely by naming the file\n$part = File::buildFileContent($binary, 'upload.png', 'image/png');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass filename and content type when building DataPart/File from raw contents","Health-check the temp dir in deployment smoke tests (is_writable + disk_free_space)","Set sys_temp_dir to a known writable path in hardened environments"],"tags":["php","easywechat","file-upload","tempfile","environment"],"backgroundTag":"temp-file-creation-failed","analyzedSha":"f0cf0a8b8361417ed683b8246d0ecbaf0aafcaa8","analyzedAt":"2026-08-21T05:29:19.565Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}