Anik Biswas
Anik Biswas

Reputation: 135

how to delete a file or a photo from the folder when deleting it from database?

hi i'm try to delete image from folder but i'm getting this error message

Class 'App\Http\Controllers\File' not found

when i dd() the image path it look like this : "C:\xampp\htdocs\blog\public\/images/1544525527.jpg" here is my delete code:

 $post = Post::find($id);
$file= $post->image;
$destinationPath = public_path('/images');
$filename = $destinationPath.'/'.$file;
File::delete($filename);

and i upload image like this:

 if ($request->hasFile('image')) {
            $image = $request->file('image');
            $name = time().'.'.$image->getClientOriginalExtension();
            $destinationPath = public_path('/images');
            $image->move($destinationPath, $name);
            $post->image = url('/public/images/').'/'.$name;
        }

Upvotes: 1

Views: 288

Answers (3)

dhara gosai
dhara gosai

Reputation: 92

direct delete from your directory please try this will use in your controller

use File;

that also use in your controllers function

   $update = tablename::where('id',$request->input('uid'))->where('status',1)->first();
    if ($request->hasFile('cover'))
    {
         File::delete(public_path().$update->pu_cover_photo);
    }

Upvotes: 0

hifebriansyah
hifebriansyah

Reputation: 341

there are two solutions for this, first you can import the class by add this code in controller file

use Illuminate\Support\Facades\File;

or alternatively you can use the full path class in your code, so your code should be like this,

$post = Post::find($id);
$file= $post->image;
$destinationPath = public_path('/images');
$filename = $destinationPath.'/'.$file;

//fullpath class
\Illuminate\Support\Facades\File::delete($filename);

Upvotes: 0

Peter
Peter

Reputation: 1725

Try importing the class at the top of your controller file:

use Illuminate\Support\Facades\File;

Upvotes: 1

Related Questions