How to delete file from public folder in laravel 5.1

后端 未结 17 1673
悲哀的现实
悲哀的现实 2020-11-30 09:02

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. t

17条回答
  •  一向
    一向 (楼主)
    2020-11-30 09:38

    Its a very old thread, but I don't see that the solution is here or the this thread is marked as solved. I have also stuck into the same problem I solved it like this

      $path = public_path('../storage/YOUR_FOLDER_NAME/YOUR_FILE_NAME');
      if (!File::exists($path)) 
      {
        File::delete(public_path('storage/YOUR_FOLDER_NAME/YOUR_FILE_NAME'));
      }
    

    The key is that you need to remove '..' from the delete method. Keep in mind that this goes true if you are using Storage as well, whether you are using Storage of File don't for get to use them like

    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use File; // For File
    use Storage; // For Storage
    

    Hope that it will help someone.

提交回复
热议问题