首页 > 解决方案 > Storage::Delete 路径位置未被覆盖

问题描述

[在此处输入图片描述][1]不是这样吗?但它并没有删除图像,尽管它存储了图像

如果不是这样,那么我该如何覆盖控制器中的公共路径?

已编辑(如建议):我在我的 filesystem.php 中添加了新磁盘“public”,并通过添加 Storage facade 修改了控制器。


在我的controller.php

public function update(Request $request, $userId)
{
    $oldFileName='';
    $filenameToStore='';
    //saving the user
    $user=User::find($userId);
    $user->first_name = $request->input('first_name');

    //for the image
    if ($request->hasFile('user_image')) {
        $image = $request->file('user_image');

        //get filename
        $filenameToStore= $image->getClientOriginalName();

       $location = public_path('chbea/users/images/' .$filenameToStore);

        Image::make($image)->save($location);
        $oldFileName =$user->user_image;

        Storage::disk('public')->delete('chbea/users/images/'. 
        $oldFileName);

        //saving user image name
        $user->user_image = $filenameToStore;
    }

    $user->save();
    Session::flash('success', 'The Student Details was Updated!');
    return redirect()->route('students.index');


}

在我的 filesystems.php

<?php

return [


'default' => env('FILESYSTEM_DRIVER', 'local'),


'cloud' => env('FILESYSTEM_CLOUD', 's3'),


'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => public_path('images/'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => public_path(),
    ],

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

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],

  ],

 ];

当我 dd 输出时,路径不是预期的。

dd(Storage::disk('public'), 
  Storage::disk('public')>path('chbea/users/images/'.$oldFileName),
  Storage::disk('public')->exists('chbea/users/images/'.$oldFileName)
 [enter image description here][1]);

这是输出 https://i.stack.imgur.com/euVWR.png

标签: laravelimagelocal-storagestorage

解决方案


所以这里的问题是,当您像在filesystems.php配置中那样设置文件系统时,它会将该文件系统囚禁到您定义的路径中。因此,在您的情况下,您的local文件系统被监禁到public_path('allimages/'). 您不能使用该磁盘超出该文件系统。

但是,您可以创建另一个被监禁到不同位置的磁盘:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => public_path('allimages/'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => public_path()
    ],

在这里,我创建了另一个名为的磁盘public并将该磁盘监禁到公共路径。然后,您可以像这样使用存储外观:

Storage::disk('public')->delete('project/users/images/' . $oldFileName);

这将删除由您给出的路径标识的文件,相对于public磁盘的根目录。


推荐阅读