laravel 保存 pdf 没有那个文件或目录

laravel save pdf no such file or directory

所以我想将一个pdf文件保存到我本地服务器上的一个目录中,但它一直说该目录不存在。

首先,您将在哪里存储外部无法访问的 PDF 文件(因此不在 public 文件夹中)。

所以这是我的代码。下载完美无缺。

public function generatePDF()
    {
        $this->mailorder = Session::get('order');
        $this->cart = Session::get('cart');
        $data = [
                    'id' => $this->mailorder->id,
                    'client' => $this->mailorder->Contact,
                    'country' => $this->mailorder->country,
                    'city' => $this->mailorder->city,
                    'street' => $this->mailorder->street,
                    'postal' => $this->mailorder->postal,
                    'phone' => $this->mailorder->phone,
                    'email' => $this->mailorder->email,
                    'dateIn' => $this->mailorder->dateIn,
                    'dateOut' => $this->mailorder->dateOut,
                    'subtotal' => $this->mailorder->subtotal,
                    'tax' => $this->mailorder->tax,
                    'total' => $this->mailorder->total,
                    'cart' => $this->mailorder->cart,
                    'delivery' => $this->mailorder->delivery,
                ];
        $path = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";       
        $pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save('storage/app/public/'.$path.'.pdf');
        ;

        return $pdf->download(''.$path.'.pdf');
    }

首先,您应该检查该目录是否存在File facade。如果不存在,则必须创建该目录。

if(!File::exists($directory_path)) {
    File::makeDirectory($directory_path);
}

如果还是报错,必须强行制作目录:

if(!File::exists($directory_path)) {

   File::makeDirectory($directory_path, $mode = 0755, true, true);    
}

之后,您可以将文件保存在该目录中。

其次,如果不想将文件保存在public目录下。您必须将其保存在 storage.By 中,只需调用 storage_path($file_path)。这样 laravel 将文件保存在 storage/app/public 目录下。

之后就可以根据这个得到文件的URL了。

我明白了谢谢你的回答。

这是我的代码:

public function generatePDF()
    {
        $this->mailorder = Session::get('order');
        $this->cart = Session::get('cart');
        $data = [
                    'id' => $this->mailorder->id,
                    'client' => $this->mailorder->Contact,
                    'country' => $this->mailorder->country,
                    'city' => $this->mailorder->city,
                    'street' => $this->mailorder->street,
                    'postal' => $this->mailorder->postal,
                    'phone' => $this->mailorder->phone,
                    'email' => $this->mailorder->email,
                    'dateIn' => $this->mailorder->dateIn,
                    'dateOut' => $this->mailorder->dateOut,
                    'subtotal' => $this->mailorder->subtotal,
                    'tax' => $this->mailorder->tax,
                    'total' => $this->mailorder->total,
                    'cart' => $this->mailorder->cart,
                    'delivery' => $this->mailorder->delivery,
                ];
        $filename = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
        $path = storage_path('pdf/orders');

        if(!File::exists($path)) {
            File::makeDirectory($path, $mode = 0755, true, true);

        } 
        else {}

        $pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save(''.$path.'/'.$filename.'.pdf');
        ;

        return $pdf->download(''.$filename.'.pdf');
    }