Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file from a specific directory in Laravel local storage

Tags:

file

php

laravel

I am storing files at local storage. So, in /storage/app/public directory.

I am storing my files in /storage/app/public/userId/images ;

I used php artisan storage:link , so I can access that files in view, having a shortcut to this folder in /public/storage/userId/images Inside that path I have 2 images - test.jpg and test2.jpg

I can't find a response at Laravel documentation, how to delete file test.jpg from /public/storage/userId/images

I tried in this way :

$path = 'public/' . $id . '/diploma';
$files =  Storage::files($path);
return $files;

It returns me :

[
"public/303030/images/test.jpg"
"public/303030/images/test2.jpg"
]

Now, how can I call Storage::delete('test.jpg') on that array?

like image 997
priMo-ex3m Avatar asked Jan 18 '18 09:01

priMo-ex3m


People also ask

How do I delete files from storage folder in Laravel?

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.

How do you delete a file in Laravel?

You could use PHP's unlink() method just as @Khan suggested. But if you want to do it the Laravel way, use the File::delete() method instead. $files = array($file1, $file2); File::delete($files);

How do I delete files from storage?

Locate the file that you want to delete. Right-click the file, then click Delete on the shortcut menu. Tip: You can also select more than one file to be deleted at the same time. Press and hold the CTRL key as you select multiple files to delete.


2 Answers

There is multiple ways to delete image

//In laravel 
File::delete($image);
//for specific directory
File::delete('images/' . 'image1.jpg');

and other way (Simple PHP)

//Simple PHP
unlink(public_path('storage/image/delete'));

and if you want to delete more than 1 images than

Storage::delete(['file1.jpg', 'file2.jpg']);
//or
File::delete($image1, $image2, $image3);

for more detail about Delete File in Laravel

like image 100
Bilal Ahmed Avatar answered Oct 20 '22 16:10

Bilal Ahmed


Use Storage::delete(). The delete method accepts a single filename or an array of files to remove from the disk.

Storage::delete($file_to_delete);

May be you want to do something like-

$files =  Storage::files($path);
Storage::delete($files);
like image 32
Sohel0415 Avatar answered Oct 20 '22 17:10

Sohel0415