Sunday, 15 August 2010

folder - Remove empty subfolders with PHP -


I am working on a PHP function which will remove all sub-folders repeatedly, There are no files starting with the path

Even the code has been developed:

  function RemoveEmptySubFolders ($ start_from_path) {// Returns if the folder does not have a function function isEmptyFolder ( $ Folder) {return (calculation (array_diff (globe $ folder.DIRECTORY_SEPARATOR. "*"), Array (".", ".."))) == 0); } // if there is at least one empty folder function DoRemoveEmptyFolders ($ from_path) {if (IsEmptyFolder ($ from_path)) {rmdir ($ from_path)) has been removed via cycling $ from $ subfolders and // returns true; Back true; } And {$ Dirs = glob ($ to $ _path.DIRECTORY_SEPARATOR. "*", GLOB_ONLYDIR); $ Ret = false; Forex currency ($ path $ as path) {$ res = DoRemoveEmptyFolders ($ path); $ Ret = $ ret? $ Ret: $ res; } Return $ ret; }} While doing this (DoRemoveEmptyFolders ($ start_from_path)) {}}  

This function works according to my test, although I would be happy to see any ideas for better performance code .

All of this, because before you check your children, first go to the test folder. Instead, you should go to the hair folder before testing the parents, if empty, so that one pass will be enough.

  RemoveEmptySubFolders ($ path) function {$ empty = true; Foreach ($ path. DIRECTORY_SEPARATOR. "*") $ As file) {if (is_dir ($ file)) {if (! RemoveEmptySubFolders ($ file)) $ empty = false; } And ($ empty = false;}} if ($ empty) rmdir ($ path); return $ empty;}  

By the way, the globe does not return. And .. entries.

short version:

  function RemoveEmptySubFolders ($ path) {$ empty = true; foreign currency (globe $ DIRECTORY_SEPARATOR. "*") $ File format ) $ $ Empty & amp; = is_dir ($ file) & amp; amp; amp; RemoveEmptySubFolders ($ file);} return $ blank & amp; amp; rmdir ($ path);}  
< / Div>

No comments:

Post a Comment