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; } }