How to delete file from public folder in laravel 5.1

后端 未结 17 1628
悲哀的现实
悲哀的现实 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.

    0 讨论(0)
  • 2020-11-30 09:40

    Try it :Laravel 5.5

    public function destroy($id){  
          $data = User::FindOrFail($id);  
          if(file_exists('backend_assets/uploads/userPhoto/'.$data->photo) AND !empty($data->photo)){ 
                unlink('backend_assets/uploads/userPhoto/'.$data->photo);
             } 
                try{
    
                    $data->delete();
                    $bug = 0;
                }
                catch(\Exception $e){
                    $bug = $e->errorInfo[1];
                } 
                if($bug==0){
                    echo "success";
                }else{
                    echo 'error';
                }
            }
    
    0 讨论(0)
  • 2020-11-30 09:40

    the easiest way for you to delete the image of the news is using the model event like below and the model delete the image if the news deleted

    at first you should import this in top of the model class use Illuminate\Support\Facades\Storage after that in the model class News you should do this

    public static function boot(){
        parent::boot();
    
        static::deleting(function ($news) {
              Storage::disk('public')->delete("{$news->image}");
        })
    }
    

    or you can delete the image in your controller with this command

    Storage::disk('public')->delete("images/news/{$news->file_name}");
    

    but you should know that the default disk is public but if you create folder in the public folder and put the image on that you should set the folder name before $news->file_name

    0 讨论(0)
  • 2020-11-30 09:41

    Follow the steps carefully to get the image first=>

        $img = DB::table('students')->where('id',$id)->first();
        $image_path = $img->photo;
        
        unlink($image_path);
        DB::table('students')->where('id',$id)->delete();
    
    0 讨论(0)
  • 2020-11-30 09:43

    Update working for Laravel 8.x:

    Deleting an image for example ->

    First of all add the File Facade at the top of the controller:

    use Illuminate\Support\Facades\File;
    

    Then use delete function. If the file is in 'public/' you have to specify the path using public_path() function:

    File::delete(public_path("images/filename.png"));
    
    0 讨论(0)
提交回复
热议问题