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 28, 2020 in Laravel by kartik
• 37,520 points
27,565 views

2 answers 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 28, 2020 by Niroj
• 82,840 points
0 votes

I just want to add @niroj answer 

Use the unlink function of php, just pass exact path to your file to unlink function :

unlink($file_path);

Do not forget to create complete path of your file if it is not stored in the DB. e.g

$file_path = app_path().'/images/news/'.$news->photo;
answered Nov 5, 2020 by Helloworld
• 140 points

Related Questions In Laravel

0 votes
1 answer

How can I serve a single HTML page from the Laravel public folder without having to use the .html extension?

Hello @kartik, You may rename the test folder ...READ MORE

answered Nov 12, 2020 in Laravel by Niroj
• 82,840 points
5,633 views
0 votes
2 answers

How to delete or clear caching in Laravel?

You can call an Artisan command outside ...READ MORE

answered Dec 16, 2020 in Laravel by Rajiv
• 8,870 points
95,590 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, 2020 in Laravel by Niroj
• 82,840 points
3,273 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, 2020 in Laravel by Niroj
• 82,840 points
3,145 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, 2020 in JQuery by Niroj
• 82,840 points
23,380 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, 2020 in JQuery by Niroj
• 82,840 points
3,339 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, 2020 in JQuery by Niroj
• 82,840 points
5,141 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, 2020 in JQuery by Amit
• 140 points
2,976 views
+1 vote
4 answers

How to access images inside public folder in laravel?

If you are inside a blade template {{ ...READ MORE

answered Dec 14, 2020 in Laravel by Rajiv
• 8,870 points
132,029 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 28, 2020 in Laravel by Niroj
• 82,840 points
9,659 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP