How to delete file from public folder in laravel?

0 votes

I want to delete a News from database and when I hit the delete button all data from database deleted but the image is remains in upload folder. So, how do I this to work?

This is my function again but does not delete the image from images/news folder of public directory>

 public function destroy($id) {
    $news = News::findOrFail($id);
    $image_path = app_path("images/news/{$news->photo}");

    if (File::exists($image_path)) {
        //File::delete($image_path);
        unlink($image_path);
    }
    $news->delete();
    return redirect('admin/dashboard')->with('message','Hello');
}
Sep 27 in Laravel by kartik
• 30,630 points
30 views

1 answer to this question.

0 votes

Hello @kartik,

You could use PHP's unlink() method just as @Khan suggested.

But if you want to do it the Laravel way, use the File::delete() method instead.

 Delete a single file

File::delete($filename);

Delete multiple files

File::delete($file1, $file2, $file3);

Delete an array of files

$files = array($file1, $file2);
File::delete($files);

And don't forget to add at the top:

use Illuminate\Support\Facades\File; 

Hope it helps!!

ThanK you!!

answered Sep 27 by Niroj
• 66,420 points

Related Questions In Laravel

0 votes
1 answer

How to delete or clear caching in Laravel?

Hey @kartik, There are many commands to clear ...READ MORE

answered Mar 20 in Laravel by Niroj
• 66,420 points
5,149 views
0 votes
1 answer

How to get data from Laravel backend and display in Vue/Nuxt frontend

Hello, You have to use the package dotenv. Then ...READ MORE

answered Apr 8 in Laravel by Niroj
• 66,420 points
736 views
0 votes
1 answer

How to populating a database in a Laravel migration file?

Hello @kartik, Don't put the DB::insert() inside of ...READ MORE

answered Aug 4 in Laravel by Niroj
• 66,420 points
73 views
0 votes
1 answer

How to get public directory in laravel?

Hello @kartik, Use public_path() For reference: // Path to the project's ...READ MORE

answered Aug 10 in Laravel by Niroj
• 66,420 points
1,381 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28 in JQuery by Niroj
• 66,420 points
3,061 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12 in JQuery by Niroj
• 66,420 points
226 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12 in JQuery by Niroj
• 66,420 points
803 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7 in JQuery by Amit
• 140 points
227 views
+1 vote
2 answers

How to access images inside public folder in laravel?

You simply need to use the asset helper function ...READ MORE

answered Aug 27 in Laravel by Dsouza
• 150 points
9,897 views
0 votes
1 answer

How to upload files in Laravel directly into public folder?

Hello @kartik, You can create a new storage ...READ MORE

answered Sep 27 in Laravel by Niroj
• 66,420 points
61 views