Laravel의 공용 폴더에서 파일을 삭제하는 방법

Laravel의 공용 폴더에서 파일을 삭제하는 방법

2022-10-05 last update

4 minutes reading laravel public file delete
  • 이 게시물에서는 공용 저장소에서 파일을 제거/삭제하는 방법을 살펴보겠습니다. 오늘은 라라벨의 저장소 폴더에서 파일을 제거하는 방법에 대한 데모를 보여 드리겠습니다.
  • 그래서 여기서는 Laravel File System과 PHP 함수 file_exists(), unlink()를 이용하여 저장 폴더에서 이미지를 삭제하는 방법을 설명하겠습니다.
  • 이미지 또는 파일 업로드는 웹 개발에서 동일한 기능입니다. 여러 번 데이터베이스에서 이미지나 파일을 삭제해야 하지만 이러한 파일은 여전히 ​​라라벨 스토리지 기능에 저장됩니다.
  • 아무 조치도 취하지 않으면 더 많은 공간을 차지하므로 수동으로 제거하거나 laravel 함수 또는 핵심 PHP 함수를 통해 제거할 수 있습니다.

  • Using Storage System



    public function removeImage()
    {  
      if(\Storage::exists('upload/img.png')){
        \Storage::delete('upload/img.png');
      }else{
        dd('File not found.');
      }
    }
    


    Using File System



    public function removeImage()
    {  
      if(\File::exists(public_path('upload/img.png'))){
        \File::delete(public_path('upload/img.png'));
      }else{
        dd('File not found');
      }
    }
    


    Using PHP



    public function removeImage()
    {  
        if(file_exists(public_path('upload/img.png'))){
          unlink(public_path('upload/img.png'));
        }else{
          dd('File not found');
        }
    }
    



    Read Also : Google Recaptcha Example In Laravel



    읽어 주셔서 감사합니다 !!