0

If an item no longer exists in an array but there is a folder with the no longer required name. What would be the best way to check and remove the folder?

Example:

I have an array('blah','apple','oneDirection'); I will then run a command to create a folder/dir in a specific location with the names in the array but if oneDirection is no longer in the array I would like to delete the folder/dir -> Would I use in_array?

The array data comes from an external JSON feed so it will be removed automatically and when this is done I would like it to delete the folder/dir - I am not sure on the array and folder/dir name checking structure

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jess McKenzie
  • 8,345
  • 27
  • 100
  • 170

4 Answers4

1

Just a quick snippet that might serve as an example.

function del_dir($dir, $key, $root) {
        // may need to do more here like delete recursively
        $rp= realpath($root .'\\'. $dir);
        rmdir( $rp);
        echo "folder: $rp : removed\n";
}

// your root folder
$home_dir= "c:\\tmp";
// your array of folders
$folders= array("folder1", "folder2");
$fs_folders=array();
if ($dh = opendir($home_dir)) {
    while (($f = readdir($dh)) !== false) {
        if(is_dir("$home_dir\\$f")){
            if ($f != "." && $f != "..")
                $fs_folders[]= $f;
        }
    }
    closedir($dh);
}

$todel= array_diff($fs_folders, $folders);
array_walk($todel, "del_dir", $home_dir);
renick
  • 3,873
  • 2
  • 31
  • 40
0

TMHO, the same place in the code that "removes" the folder from the array - should delete the real folder. To be more specific, instead of using unset or array_pop - create a function that first tries to rmdir the folder (using try and catch) and only if everything went well - it should remove the item from the array.

Further, in case the folder cannot be deleted at that time, the item should not be removed - but keep in "hold" in some kind of pending queue - with retries etc.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • The code comes from a JSON feed form an external source. I am just a bit lost on the checking array and checking the folder/dir names – Jess McKenzie Nov 30 '12 at 07:30
  • @JessMcKenzie in that case I would create a new array with *candidates for deletion* and handle them one at a time, in the same way I mentioned above. – Nir Alfasi Nov 30 '12 at 07:32
  • Can you provide me an example I am guessing I would have an array with the $id and an array with the folder/dir names and if they do not match removes the folder? – Jess McKenzie Nov 30 '12 at 07:36
0
  1. When you delete an item from array, delete the folder too; if it is not possible
  2. Keep two arrays: In one, items are removed, then compare arrays for differencies = folders to delete, or
  3. if all folders are in one folder with no other content, load all childs of the parent folder and check whether they're in the array

Here's how to get all folders of another folder: PHP opendir() to list folders only

Then, in if statement use in_array method or other way to determine if the folder is in array. If not, delete it.

Community
  • 1
  • 1
jcjr
  • 1,503
  • 24
  • 40
  • I am after the structure for ur first point - The data in the array comes from an external source so it will automatically be removed. I am just not sure the best way to check the array and folder/dir names – Jess McKenzie Nov 30 '12 at 07:33
0

What you have to do, is loop over all the folders you currently have, and store all of their names/id's in an array - We'll call this array, $createdFolders.

Then loop over the JSON feed, and create all of the folders that is not yet created, and unset from the id/name from $createdFolders, if it already exists.

When you have looped over all the items, you will be able to loop over $createdFolders. The items left here, are the delete candidates.

As for the actual checking, you can use in_array, something like this should suffice:

if ( in_array ( $array[$item], $createdFolders )) {
    unset ( $createdFolders[$item] );
}

Ofcourse, this assumes that the key is the same for both arrays.

Kao
  • 2,242
  • 3
  • 22
  • 31