我有一个Laravel项目,我试图用Storage::delete删除图像,但它似乎不工作。我的图片在storageapppublicuploads文件夹中。我能够上传和编辑图像,但删除不工作。我用的是Laravel 8。
PRODUCTSCONTROLLER.PHP
<?php
namespace AppHttpControllers;
use AppModelsProduct;
use IlluminateHttpRequest;
use IlluminateSupportFacadesStorage;
class ProductsController extends Controller
{
public function store(Request $request){
request()->validate([
'product_name' => ['required', 'min:2', 'max:50'],
'product_price' => 'required',
'product_desc' => ['required', 'min:2', 'max:50'],
'product_img' => 'image|nullable|max:1999'
]);
// Handle File Upload
if($request->hasFile('product_img')){
// Get filename with the extension
$filenameWithExt = $request->file('product_img')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('product_img')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('product_img')->storeAs('public/uploads', $fileNameToStore);
}
$product = new Product();
$product->product_name = request('product_name');
$product->product_price = request('product_price');
$product->product_desc = request('product_desc');
if($request->hasFile('product_img')){
$product->product_img = $fileNameToStore;
}
$product->save();
return redirect('/posts');
}
public function destroy($id){
$product = Product::findOrFail($id);
Storage::delete('public/uploads' . $product->product_img);
$product->delete();
return redirect('/products');
}
}
PRODUCTS.BLADE.PHP
@extends('layout.mainlayout')
@section('content')
<div class="product-wrapper">
<div class="container">
<div class="row">
@foreach ($products as $product)
<div class="col-md-4 col-12">
@auth
<a href="/products/edit/{{ $product->id }}" class="btn btn-light">Edit</a>
<a href="/products/delete/{{ $product->id }}" class="btn btn-danger">Delete</a>
@endauth
</div>
@endforeach
</div>
</div>
</div>
@endsection
WEB.PHP
Route::get('/products',[AppHttpControllersProductsController::class,'products']);
Route::post('/products/store',[AppHttpControllersProductsController::class,'store'])->middleware('auth');
Route::get('/products/delete/{product}', [AppHttpControllersProductsController::class,'destroy'])->middleware('auth');
配置/FILESYSTEMS.PHP
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'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'),
'endpoint' => env('AWS_ENDPOINT'),
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
File::delete(public_path('storage/'.$imagenamehere));
试试这段代码,也要确保你添加了File类。