Laravel存储::get()不工作,返回空值



我想在laravel存储文件夹中获取图像文件,并将其编码为base64。如果我使用这个代码,它总是返回空值

$data = base64_encode(Storage::disk('public')->get('/customer/Avatars',"image.png"));

在localhost中,此代码适用于

$data = base64_encode(file_get_contents(public_path().'/storage/customer/Avatars/'."image.png"));

但在上传到cpanel后,上面的代码给了我这个错误

ErrorException: file_get_contents(/home/site/wwwroot/public/storage/customer/Avatars/image.png): failed to open stream: No such file or directory in file /home/site/wwwroot/app/Http/Controllers/Customer/CustomerProfileController.php on line 56

如何获取此文件?

您可以使用laravel全局存储路径助手在存储文件夹上找到任何文件路径

$imagePath=storage_path('customer/Avatars/image.png');

如果您在公用文件夹中获得文件路径

$imagePath=public_path('customer/Avatars/image.png');

这件事发生在我身上,我想分享它,因为你们中的一些人可能也有同样的问题,我试图使用Storage::get((方法对文件进行编码,但它返回了一个空字段,最后我意识到我用该方法获得的文件内部是空的,所以请确保你的伪文件内部有一些内容,否则它将始终返回空。

最新更新