【问题标题】:Laravel Storage::disk()->url(); does not work properlyLaravel 存储::disk()->url();不能正常工作
【发布时间】:2017-12-21 07:04:17
【问题描述】:

您好,我在 laravel 中遇到了问题。

我想为一些东西(xls、图像、pdf 等)创建私有存储。 在 storage/app/public 目录中一切正常,但我不想要它,我想要我的目录,如 storage/app/products/{id}/

先看看我的代码:

文件系统.php

   'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

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

我创建了新的数组“产品”

ProductionController.php

public function file()
{

   return '<img src="'.Storage::disk('productions')->url('7/2.png').'">';

}

web.php(路由)

Route::group([
'middleware'=>'roles',
'roles'=>['Administrator','Prefabrykacja','Dyrektor Prefabrykacji']
], function () {
    Route::get('/Produkcja',[
        'uses'=>'ProductionController@index',
        'as'=>'production.index']);
    Route::post('/Produkcja/create',[
        'uses'=>'ProductionController@create',
        'as'=>'production.create']);


    Route::get('/Produkcja/file',[
        'uses'=>'ProductionController@file',
        'as'=>'production.file']);
});

如果我回来

return '<img src="'.Storage::disk('productions')->url('7/2.png').'">';

或者

return '<img src="'.Storage::disk('local')->url('7/2.png').'">';

结果是一样的。这两行都从 storage/app/public/7/2.png 返回图像将不显示图像 storage/app/products/7/2.png

如何显示我的产品文件夹中的图像并将资源限制为指定角色?

问候

【问题讨论】:

    标签: php laravel laravel-5.4


    【解决方案1】:

    首先,您可能缺少public (Local URL Host Customization) 的符号链接。此外,您需要指定url 参数,如public 并将visibility 更改为public 以允许其他人查看您的文件。

    文件系统.php:

    'disks' => [
    
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
    
        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],
    
        'productions' => [
            'driver' => 'local',
            'root' => storage_path('app/productions'),
            'url' => env('APP_URL') . '/productions', // added line (directory within "public")
            'visibility' => 'public', // modified visibility
        ],
    ],
    

    还要确保在 public/productions 创建一个符号链接,它将指向 storage/app/productions 目录。您可以使用以下命令来执行此操作(例如):

    cd LARAVEL_PATH && ln -s storage/app/productions public/productions
    

    【讨论】:

    • 如果我做 Storage::disk('productions')->put('file.txt', 'Contents');一切正常,他们在 storage/app/productions 中创建 file.txt 并将“内容”放入其中,但仍无法从该目录读取图像。在我使用命令 php artisan storage:link 之前,如果我尝试使用您的命令,它会说“ln:无法创建符号链接 'public/productions':没有这样的文件或目录”,我尝试将您的命令适应我的项目。跨度>
    • 你的 LARAVEL_PATH 根目录看起来如何?提供带有 ls -la LARAVEL_PATH 输出的 pastebin 链接。
    猜你喜欢
    • 2022-01-13
    • 2014-01-26
    • 1970-01-01
    • 2015-09-29
    • 2017-08-09
    • 2018-07-30
    • 2020-12-21
    • 2018-09-14
    • 2018-04-05
    相关资源
    最近更新 更多