0

i have a laravel project and i want to delete image from this directory

myLaravelProject/public/images/banners

Storage::disk('public')->delete("images/banners/{$image_name}");
verisimilitude
  • 5,077
  • 3
  • 30
  • 35
arash kenji
  • 31
  • 1
  • 3
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – N69S Oct 08 '19 at 10:25
  • in other words, you dont need those brackets `{ }` around your variable as it will double interpret the variable. Or simply use `'/images/banners/'.$image_name` – N69S Oct 08 '19 at 10:26
  • @N69S I don't think that's a relevant dupe, as it'll work just the same (see https://3v4l.org/j5Z2t). – Qirel Oct 08 '19 at 10:33
  • @arashkenji Have you ensured that `$image_name` is what you expect it to be? – Qirel Oct 08 '19 at 10:33
  • find the path of your file from public (you can use `public_path()` OR `asset('')`), then do this: `unlink($file_path);`. – Reza sh Oct 08 '19 at 11:11

3 Answers3

0

For good practice you need to edit config with a new storage disc in config/filesystems.php:

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

and then

 Storage::disk('public_uploads')->delete("{$image_name}");
0

Something like this works for me:

use File;

$file_path = public_path(substr($this->path, 1));

if(File::exists($file_path)) {
    File::delete($file_path);
}
Patrick
  • 781
  • 2
  • 5
  • 23
0

For Laravel 8, this code works for me:

use Illuminate\Support\Facades\File; 


$file_path = public_path('upload/test.png');

if(File::exists($file_path)) {
    File::delete($file_path);
} 

I hope it helps!