Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a folder with files in Laravel

What is the proper syntax when deleting a folder with files in laravel 5.2?

Here is my syntax when deleting the folder from the DB and DIR

$folder = Folder::find($id);
$folder_path = storage_path('locker').'/'. $folder->folder_title;
$folder->delete();
 `rmdir($folder_path);`
\Session::flash('success', 'Folder Deleted!');
return back();
like image 763
Mark Santos Avatar asked Dec 18 '16 11:12

Mark Santos


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 directory in Laravel?

The public_path() is a method Laravel uses to fetch the folder passed as a parameter to the path of the directory. The Storage facade grants access to the file system of the application so that the deleteDirectory() method can delete the specified directory.

Can I delete storage folder in Laravel?

To delete a directory in the Laravel application you can make use of the "deleteDirectory()" method available from the Storage facade. <? php Storage::deleteDirectory($directory); This method will delete the directory with all of its files so you must carefully ensure that you are deleting the right directory.

How do I delete files in Laravel?

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


2 Answers

After much frustration I got the solution.

Use 'File' instead of 'Storage'

File::deleteDirectory($path);
like image 113
Adiyya Tadikamalla Avatar answered Oct 05 '22 19:10

Adiyya Tadikamalla


The deleteDirectory may be used to remove a directory and all of its files

Storage::deleteDirectory($directory);

https://laravel.com/docs/5.3/filesystem#directories

like image 37
Alexey Mezenin Avatar answered Oct 05 '22 18:10

Alexey Mezenin