首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel5.7: pdf文件存储在文件夹中

Laravel5.7: pdf文件存储在文件夹中
EN

Stack Overflow用户
提问于 2019-08-06 11:31:52
回答 1查看 859关注 0票数 0

我试图在文件夹中存储一个pdf文件,但是无论我是使用Laravel来获取文件还是通过它的验证(表单请求),都会发生奇怪的事情。

假设我得到了pdf文件并按如下方式存储:

代码语言:javascript
复制
$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);
}

假设$ordercat1,文件将按如下方式存储:

>:文件夹-:文件

代码语言:javascript
复制
>orders
  >cat1
    >1234567890($id)
      -cat1.pdf
      -cat1.xml

这正是它的结果,但cat1.pdf尚未成功上传,如果我试图打开它,我会得到一个错误。

但是,当我得到带有Laravel函数的文件时,检查它是否上传成功(在文档中找到),如下所示:

代码语言:javascript
复制
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);
       }

然后,该文件将按如下方式存储:

代码语言:javascript
复制
>orders
  >cat1
    >1234567890($id)
      >cat1.pdf
        > nVeY7HjwLLy[...].pdf (random id by Laravel)
      -cat1.xml

像这样的pdf文件被成功上传,我可以打开它。

但出于一个原因,我不知道pdf文件是存储在另一个文件夹,即使我使用完全相同的方式存储它。

,这两种获取pdf文件的方法有什么不同,它是如何影响文件存储的?

EN

回答 1

Stack Overflow用户

发布于 2019-08-06 13:36:50

对于上传的文件,您应该使用store / storeAs (laravel)或move (交响乐)方法来存储该文件。

代码语言:javascript
复制
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方法,您还可以指定希望它转到的磁盘。更多信息这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57375078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档