我试图在文件夹中存储一个pdf文件,但是无论我是使用Laravel来获取文件还是通过它的验证(表单请求),都会发生奇怪的事情。
假设我得到了pdf文件并按如下方式存储:
$file = $request->validated(); <-
$uuid = Uuid::uuid4();
$id = $uuid->toString();
if ($order == 'cat1') {
$path = '/orders/cat1/' . $id;
Storage::makeDirectory($path, 0755, true, true);
Storage::put($path.'/cat1.xml', $xml);
Storage::put($path.'/cat1.pdf', $file);
}
elseif (empty($order) || $order == 'cat2') {
$path = '/orders/cat2/' . $id;
Storage::makeDirectory($path, 0755, true, true);
Storage::put($path.'/cat2.xml', $xml);
Storage::put($path.'/cat2.pdf', $file);
}假设$order是cat1,文件将按如下方式存储:
>:文件夹-:文件
>orders
>cat1
>1234567890($id)
-cat1.pdf
-cat1.xml这正是它的结果,但cat1.pdf尚未成功上传,如果我试图打开它,我会得到一个错误。
但是,当我得到带有Laravel函数的文件时,检查它是否上传成功(在文档中找到),如下所示:
if ($request->hasFile('file')) {
$file = $request->file('file'); <-
if ($file->isValid()) {
$uuid = Uuid::uuid4();
$id = $uuid->toString();
if ($order == 'cat1') {
$path = '/orders/cat1/' . $id;
Storage::makeDirectory($path, 0755, true, true);
Storage::put($path.'/cat1.xml', $xml);
Storage::put($path.'/cat2.pdf', $file);
}
elseif (empty($order) || $order == 'cat2') {
$path = '/orders/cat2/' . $id;
Storage::makeDirectory($path, 0755, true, true);
Storage::put($path.'/cat2.xml', $xml);
Storage::put($path.'/cat2.pdf', $file);
}然后,该文件将按如下方式存储:
>orders
>cat1
>1234567890($id)
>cat1.pdf
> nVeY7HjwLLy[...].pdf (random id by Laravel)
-cat1.xml像这样的pdf文件被成功上传,我可以打开它。
但出于一个原因,我不知道pdf文件是存储在另一个文件夹,即使我使用完全相同的方式存储它。
,这两种获取pdf文件的方法有什么不同,它是如何影响文件存储的?
发布于 2019-08-06 13:36:50
对于上传的文件,您应该使用store / storeAs (laravel)或move (交响乐)方法来存储该文件。
if ($request->hasFile('file')) {
$file = $request->file('file'); <-
if ($file->isValid()) {
$uuid = Uuid::uuid4();
$id = $uuid->toString();
if ($order == 'cat1') {
$path = '/orders/cat1/' . $id;
Storage::makeDirectory($path, 0755, true, true);
Storage::put($path.'/cat1.xml', $xml);
$file->storeAs($path, 'cat1.pdf');
}
elseif (empty($order) || $order == 'cat2') {
$path = '/orders/cat2/' . $id;
Storage::makeDirectory($path, 0755, true, true);
Storage::put($path.'/cat2.xml', $xml);
$file->storeAs($path, 'cat2.pdf');
}
}
}使用storeAs / store方法,您还可以指定希望它转到的磁盘。更多信息这里
https://stackoverflow.com/questions/57375078
复制相似问题