正如本问题标题中简要写的那样,我想将直接从 Controller 生成的 xml 文件直接保存在 Cloudinary 中,而不将其保存在本地(公共(public)文件夹中)。
当我设置代码时(因为我使用了 saveXML() 方法),我必须首先将其保存在本地,这样当它保存在 Cloudinary 上时,它会采用通过 saveXML 方法保存的文件的名称,然后保存它在 Cloudinary 上。
所以我希望文件直接保存到Cloudinary,而不是保存在公共(public)文件夹中。
可以这样做吗?
这是我的代码( Controller ):
public function fatturaxml($id)
{
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><mydoc></mydoc>');
$xml->addAttribute('version', '1.0');
$xml->addChild('datetime', date('Y-m-d H:i:s'));
$person = $xml->addChild('person');
$person->addChild('firstname', 'Mkenny');
$person->addChild('secondname', 'Mary');
$person->addChild('telephone', '000000000');
$person->addChild('email', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="503d3122293d3b353e3e29102931383f3f7e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>');
$xml->saveXML('myxml.xml');
$response = Response::make($xml->asXML(), 200);
$response->header('Cache-Control', 'public');
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Disposition', 'attachment; filename=test.xml');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Content-Type', 'text/xml');
cloudinary()->uploadFile('myxml.xml', ['folder' => 'FolderTest',])->getSecurePath();
}
我也尝试着写
cloudinary()->uploadFile($xml, ['folder' => 'FolderTest',])->getSecurePath();
但我收到以下错误: 缺少必需参数 - 文件
最佳答案
快速搜索即可在 documentation 中发现这一点:
参数:文件
类型:字符串
描述第 2 部分:
实际数据(字节数组缓冲区)。
例如,在某些 SDK 中,这可能是数据的 IO 输入流(例如,File.open(file, "rb"))
。
这应该意味着您可以执行类似 cloudinary()->uploadFile($xml, ['folder' => 'FolderTest',])->getSecurePath();
编辑
阅读SimpleXMLElement docs asXML()
方法应该返回 xml 内容。
If the filename isn't specified, this function returns a string on success and false on error.
我会尝试将其重写为 cloudinary()->uploadFile($xml->asXML(), ['folder' => 'FolderTest',])->getSecurePath();
由于这是一个字符串,它应该可以工作(我还没有尝试过)
关于php - 如何将文件直接上传到Cloudinary而不先将其保存在公共(public)文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74776495/