getPathname(), $fileName ?: $tempFileObject->getFilename(), $mimeType ?: $tempFileObject->getMimeType(), 0, true // Mark it as test, since the file isn't from real HTTP POST. ); // Close this file after response is sent. // Closing the file will cause to remove it from temp director! app()->terminating(function () use ($tempFile) { fclose($tempFile); }); // return UploadedFile object return $file; } /* create a tmp file from a raw string: https://gist.github.com/waska14/8b3bcebfad1f86f7fcd3b82927576e38*/ public static function UploadedFileFromRaw(string $fileData, string|null $fileName = null, string|null $mimeType = null): UploadedFile { // Create temp file and get its absolute path $tempFile = tmpfile(); $tempFilePath = stream_get_meta_data($tempFile)['uri']; // Save file data in file file_put_contents($tempFilePath, $fileData); $tempFileObject = new File($tempFilePath); $file = new UploadedFile( $tempFileObject->getPathname(), $fileName ?: $tempFileObject->getFilename(), $mimeType ?: $tempFileObject->getMimeType(), 0, true // Mark it as test, since the file isn't from real HTTP POST. ); // Close this file after response is sent. // Closing the file will cause to remove it from temp director! app()->terminating(function () use ($tempFile) { fclose($tempFile); }); // return UploadedFile object return $file; } /* create a tmp file from a raw string: https://gist.github.com/waska14/8b3bcebfad1f86f7fcd3b82927576e38*/ public static function UploadedFileFromUrl(string $url, string|null $fileName = null, string|null $mimeType = null): UploadedFile { // Create temp file and get its absolute path $tempFile = tmpfile(); $tempFilePath = stream_get_meta_data($tempFile)['uri']; // Save file data in file file_put_contents($tempFilePath, file_get_contents($url)); $tempFileObject = new File($tempFilePath); $file = new UploadedFile( $tempFileObject->getPathname(), $fileName ?: $tempFileObject->getFilename(), $mimeType ?: $tempFileObject->getMimeType(), 0, true // Mark it as test, since the file isn't from real HTTP POST. ); // Close this file after response is sent. // Closing the file will cause to remove it from temp director! app()->terminating(function () use ($tempFile) { fclose($tempFile); }); // return UploadedFile object return $file; } }