Reputation: 1692
I am modifying an uploaded pdf with Fpdi, in laravel. The pdf is generated perfectly, I can see it in any file explorer. But laravel built in Storage::exists() returns false. It is a pretty weird error, because php file_exists method can find the file perfectly with the same path, as well as File::exists
I am completely out of ideas. Any help would be appreciated!
The src variable is made by Storage::path()
$src = Storage::path("notes/".$this->note->id."-downgraded.pdf");
Storage::disk('public')->exists($src); //this returns false
file_exists($src); //this returns true
File::exists($src) //this returns true
Upvotes: 6
Views: 13338
Reputation: 1338
I assume that your laravel project is using the default laravel filesystem configuration.
If you use Storage:disk('public')
then the root-directory on this disk is different from Storage:path()
.
That means that you've actually retrieved $src
from one disk and checking its existence on a completely different disk.
If you use Storage
without a disk, then the default-disk will be used. By default, it is local
, therefore
$src
is something like this: /my-project/storage/app/notes/some.pdf
.
(see ./config/filesystem.php
to check your actual disk and default config)
You checked existence like this: Storage::disk('public')->exists($src)
.
This means that you look for /my-project/storage/app/notes/some.pdf
on the public-disk.
The root-directory of the public-disk is in general /my-project/storage/app/public
.
That means that the exists-method is actually checking the existence of
/my-project/storage/app/public/my-project/storage/app/notes/some.pdf
.
I assume this is not what you expected to happen.
Your exists-check should look like this:
# public disk root-directory is configured this:
# /my-project/storage/app/public
# file is actually here:
# /my-project/storage/app/public/notes/some.pdf
$src = "notes/some.pdf"
Storage::disk('public')->exists($src);
Using Storage
does not require you to know the absolute paths of files at all. Used properly you don't even need to care about the location of the files at all.
I highly recommend reading the manual here: https://laravel.com/docs/master/filesystem
Upvotes: 8
Reputation: 1692
I managed to make it work using File instead of Storage thanks to a comment made by Hassaan Ali
Upvotes: 2