laravel - 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]);
解决方案
所以这里的问题是,当您像在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
磁盘的根目录。
推荐阅读
- c++ - Qt 中的元组和 MSVC 编译器
- python - Tensorflow Tokenizer 恢复 fit_on_sequences
- python - 查询集中的注释分组
- amazon-web-services - 如何为 dynamodb 表使用端点?
- c# - 当 Accept 标头不是完全支持的媒体类型时,不选择自定义 TextOutputFormatter,但仅包含它
- flutter - 从多个下拉菜单中获取值并将它们连接为一个
- android - 将属性从包含的布局传递给布局时出错
- python - 如何为tflite(C ++)输入变量LSTM模型
- flutter - 断言失败:'items == null || 项目.isEmpty || 值 == 空 || items.where((DropdownMenuItem
item) 返回 item.value == value;}).length == 1' - python - 单击按钮但 Python Selenium 仍引发“无法定位元素”错误