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

后端 未结 8 1575
臣服心动
臣服心动 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:36

    I found the answer. This code worked for me.

    unlink(storage_path('app/foldername/'.$filename));
    
    0 讨论(0)
  • 2020-12-30 00:41

    This code worked for me.

    use Illuminate\Support\Facades\Storage;
    ....
    $storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
    if(file_exists($storagePath.$file)) unlink($storagePath.$file);
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-30 00:45

    The default root for the Storage facade is configured in config/filesystems.php and the default is:

    'disks' => [
    
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
    

    So you should use:

    Storage::delete('myfolder/'.$filename)
    
    0 讨论(0)
  • 2020-12-30 00:57

    You could either user Laravels facade Storage like this:

    Storage::delete($file);
    

    or you could use this:

    unlink(storage_path('app/folder/'.$file));
    

    If you want to delete a directory you could use this:

    rmdir(storage_path('app/folder/'.$folder);
    

    One important part to mention is that you should first check wether the file or directory exists or not.

    So if you want to delete a file you should probably do this:

    if(is_file($file))
    {
        // 1. possibility
        Storage::delete($file);
        // 2. possibility
        unlink(storage_path('app/folder/'.$file));
    }
    else
    {
        echo "File does not exist";
    }
    

    And if you want to check wether it is a directory do this:

    if(is_dir($file))
    {
        // 1. possibility
        Storage::delete($folder);
        // 2. possibility
        unlink(storage_path('app/folder/'.$folder));
        // 3. possibility
        rmdir(storage_path('app/folder/'.$folder));
    }
    else
    {
        echo "Directory does not exist";
    }
    
    0 讨论(0)
  • 2020-12-30 00:57

    The delete method accepts a single filename or an array of files to remove from the disk:

    use Illuminate\Support\Facades\Storage;
    
    Storage::delete('file.jpg');
    
    Storage::delete(['file.jpg', 'file2.jpg']);
    

    If necessary, you may specify the disk that the file should be deleted from:

    use Illuminate\Support\Facades\Storage;
    
    Storage::disk('s3')->delete('folder_path/file_name.jpg');
    

    Delete A Directory

    Finally, the deleteDirectory method may be used to remove a directory and all of its files:

    Storage::deleteDirectory($directory);
    
    0 讨论(0)
提交回复
热议问题