Laravel 获取存储 public 文件夹中的文件 public url

Laravel get public url for file stored in storage public folder

在 Windows 我有 Xampp。我的应用程序在 Anuglar 和 Laravel 8 我将文件添加到存储文件夹中的文件夹 public。 filesystem.php中的配置:

'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

.env 文件中:

APP_URL=http://localhost:8082/prftbx-crm-api/public

public 文件夹中的文件:

 Directory of C:\xampp\htdocs\crm-api\storage\app\public

06/25/2021  11:01 AM    <DIR>          .
06/25/2021  11:01 AM    <DIR>          ..
04/26/2021  10:57 PM                14 .gitignore
06/25/2021  11:01 AM               510 381787c3ad1442cc882ee427e93af805.txt

在Laravel我returnurl使用方法:

$fileDTO->filePath = Storage::url($fileEntity->file_name);

在视图中我有地址:

/storage/381787c3ad1442cc882ee427e93af805.txt

当我在浏览器中输入 url 时:

http://localhost:8082/crm-api/public/storage/381787c3ad1442cc882ee427e93af805.txt

我收到 404,找不到文件

第一个 运行 命令

php artisan storage:link

那么你可以

http://localhost:8082/storage/381787c3ad1442cc882ee427e93af805.txt

保持应用 url 为

APP_URL=http://localhost:8082

如文档所述

The public disk included in your application's filesystems configuration file is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores its files in storage/app/public.

To make these files accessible from the web, you should create a symbolic link from public/storage to storage/app/public. Utilizing this folder convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.

To create the symbolic link, you may use the storage:link Artisan command:

php artisan storage:link Once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:

参考:https://laravel.com/docs/8.x/filesystem#the-public-disk