-3

im using laravel 5.4 and i want to delete folder. how can i delete a folder with a prefix using file facade?

for example it is my folder and code :

$folder = public_path().'/images/carimages/something_folder1';


File::deleteDirectory($file);

and now i want to do this : delete *_folder1 (all folders which will ends with _folder1 word)

------- Soloution :

$dir = public_path().'/images/carimages/*_folder1';
$dir = glob($dir);
$dir = implode(" ",$dir);
File::deleteDirectory($dir);
K1-Aria
  • 1,093
  • 4
  • 21
  • 34

3 Answers3

1

you can delete folder by PHP function rmdir for example

if (!is_dir('examples')) {
    mkdir('examples');
}

rmdir('examples');
Gaurav Gupta
  • 1,588
  • 2
  • 14
  • 21
1

You could use the exec() method

First, get all the directories that you are looking for

$files_directory = [];
$file_search_directory = public_path()./images/carimages/';
$file_search_results = exec('find '. $file_search_directory . ' -type d -name "*_folder"', $files_directory);

Then you could loop through the files directory and delete them

foreach ($files_directory as $file_path) {
    File::deleteDirectory($file_path);
}
linktoahref
  • 7,812
  • 3
  • 29
  • 51
  • i can do it with this : array_map('rmdir', glob('*_folder1')); . is this good ? – K1-Aria Sep 22 '17 at 05:13
  • Well If that works for you, good. There's always more than one way to achieve your goal! and your method seem short and precise! Consider adding that as an answer – linktoahref Sep 22 '17 at 05:14
  • thank you . is there any different between file facade and php rmdir function in performance or security?? – K1-Aria Sep 22 '17 at 05:15
  • I don't think so there is any difference as you could check the code here https://github.com/laravel/framework/blob/5.5/src/Illuminate/Filesystem/Filesystem.php#L525 it uses `unlink` and `rmdir` internally – linktoahref Sep 22 '17 at 05:20
1

Try this

 $files = File::directories(base_path('/images/carimages'));
    foreach($files as $file) {
        if (strpos($file, '_folder1') !== false) {
            File::deleteDirectory($file);
        }
    }
Jerico Pulvera
  • 1,032
  • 7
  • 15