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

耗尽温柔 提交于 2020-08-01 09:30:11

问题


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 = url().'/storage/app/jobseekers_cvs/'.$filename;
unlink($file_path);

and

use File
$file_path = public_path().'/storage/app/myfolder/'.$filename;
unlink($file_path);

and

use File
$file_path = app_path().'/storage/app/myfolder/'.$filename;
unlink($file_path);

and also,

File::Delete('/storage/app/myfolder/'.$filename);

Please help.


回答1:


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";
}



回答2:


Use storage

//demo 
use Illuminate\Support\Facades\Storage;

Storage::delete($filename);

Another way,

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



回答3:


I found the answer. This code worked for me.

unlink(storage_path('app/foldername/'.$filename));



回答4:


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);



回答5:


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)



回答6:


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);



回答7:


Try:

unlink(public_path('uploads\users'). DIRECTORY_SEPARATOR .$user->image);

This worked for me..



来源:https://stackoverflow.com/questions/45065039/laravel-5-4-how-to-delete-a-file-stored-in-storage-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!