0

stuck in deleting the file I am saving into storage directory. My confs as shown below.

config/filesystems

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

this is how I am saving the image.

if ($request->hasFile('image'))
    {
        $image = $request->file('image');
        Storage::disk('public')->put("/uploads/".$image->hashName(),  File::get($image));
        $input['image'] = 'uploads/' . $image->hashName();
    }

I am using Laravel 7 and the image is being successfully uploaded, can anyone please suggest me the best way to remove the files stored in there.

Sodik Abdullaev
  • 158
  • 4
  • 12
  • Does this answer your question? [Laravel 5.4: how to delete a file stored in storage/app](https://stackoverflow.com/questions/45065039/laravel-5-4-how-to-delete-a-file-stored-in-storage-app) – jewishmoses Aug 06 '20 at 14:04

2 Answers2

1

If you would like Laravel to automatically manage streaming a given file to your storage location, you may use the putFile or putFileAs method. This method accepts either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance and will automatically stream the file to your desired location:

use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;

// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));

// Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');

There are a few important things to note about the putFile method. Note that we only specified a directory name, not a file name. By default, the putFile method will generate a unique ID to serve as the file name. So in your case you don't need to use $image->hasName().

putFile() also returns the path where the file was saved. So...

$path = Storage::putFile('photos', new File('/path/to/photo'));
//To delete it
Storage::delete($path);
MrEduar
  • 1,784
  • 10
  • 22
1

You can store image with:

if(request('image'))
{
     $table_column_name = request('image')->hashName();
     request('image')->store('public/uploads/');
}

You can delete image with:

$data = MODAL_NAME::findOrFail($id);
Storage::delete('public/uploads/'.$data->image);
thatguy
  • 21,059
  • 6
  • 30
  • 40