Question

I have a txt file where I list some folders I want to remove from the server (separated by comma).

The txt file contains for example:

folder1,folder1/folder2,folder1/folder2/folder3

I am trying to use rmdir to remove all the folders. The problem is that rmdir wont remove the folders if there are any folders within, and the txt file sadly list the folders in the wrong order.

Any solution? (the folders will not contain any files)

Here is the code:

$text_file = "folders.txt";

$all_folders_separated_by_comma = file_get_contents($text_file);

function not_empty_string($s) { return $s !== ""; }

$separate_all_folders = array_filter(explode(',', $all_folders_separated_by_comma), 'not_empty_string');

foreach ($separate_all_folders as $folder) {
rmdir($folder);
}
Was it helpful?

Solution

One way would be to reverse sort the folder array rsort to fix the ordering. Then check that the filename is indeed a folder with is_dir.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top