Laravel 5.4: how to delete a file stored in storage/app

后端 未结 8 1577
臣服心动
臣服心动 2020-12-30 00:22

I want to delete a file that is stored in storage/app/myfolder/file.jpg. I have tried the following codes but none of this works:

use File    
$file_path = u         


        
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 00:43

    For example update customer profile image and remove old image

    Stored file in database ($customer->image)

    /storage/customers/mhPKW870zGFupAZLI5cwNLoHTAuguCQWoBrDXJCU.jpeg
    

    Update method

    if ($request->file('image')) {
      if ($customer->image) {
          // get filename
          $filename = str_replace('/storage/customers/', '', $customer->image);
          // remove old image
          unlink(storage_path('app/public/customers/'.$filename));
      }
      // add new image
      $path = Storage::putFile('public/customers', $request->file('image'));
      $url = Storage::url($path);
    
      $customer->image = $url;
    }
           
    $saveResult = $customer->save();
    

提交回复
热议问题