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.
One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.
Tap and hold a file to select it, then tap the trash can icon, the remove button or the delete button to get rid of it.
Just place it in the controller instead of the app/start/global. php." DeleteFileAfterSend(true) works great on Laravel 5.3 as well. Although the documentation doesn't state anything about it, you can still use it.
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"; }
Use storage
//demo use Illuminate\Support\Facades\Storage; Storage::delete($filename);
Another way,
unlink(storage_path('app/folder/'.$filename));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With