【问题标题】:Laravel 8 Image deleting with Storage::delete not workingLaravel 8 使用 Storage::delete 删除图像不起作用
【发布时间】:2021-05-31 08:08:08
【问题描述】:

我有一个 Laravel 项目,我正在尝试使用 Storage::delete 删除图像,但它似乎不起作用。我的图像在 storage\app\public\uploads 文件夹中。我可以上传和编辑图像,但删除不起作用。我正在使用 Laravel 8。

PRODUCTSCONTROLLER.PHP

<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

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',[\App\Http\Controllers\ProductsController::class,'products']);
Route::post('/products/store',[\App\Http\Controllers\ProductsController::class,'store'])->middleware('auth');
Route::get('/products/delete/{product}', [\App\Http\Controllers\ProductsController::class,'destroy'])->middleware('auth');

CONFIG/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'),
    ],

];

【问题讨论】:

  • 你能附上config/filesystems.php和你的上传代码吗?
  • 我附加了config/filesystems.php并上传了代码,但还是不能解决问题
  • Storage::delete('public/uploads/' . $product-&gt;product_img);uploads后添加斜杠

标签: php laravel


【解决方案1】:
File::delete(public_path('storage/'.$imagenamehere));

试试这个代码,还要确保你添加了 File 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2018-01-21
    • 2018-10-18
    • 1970-01-01
    • 2021-11-13
    • 2014-08-28
    • 2020-03-02
    • 2016-09-30
    相关资源
    最近更新 更多