acctman Posted June 11, 2009 Share Posted June 11, 2009 i'm using the following two functions to delete all directories and to delete a single file. Can someone assist me with a function that will search all subdirectories for a specific file then delete it? function usr_del_dir($usrdir) { if (is_dir($usrdir)) $dir_handle = opendir($usrdir); if (!$dir_handle) return false; while($file = readdir($dir_handle)) { if ($file != "." && $file != "..") { if (!is_dir($usrdir."/".$file)) @unlink($usrdir."/".$file); else usr_del_dir($usrdir.'/'.$file); } } closedir($dir_handle); rmdir($usrdir); return true; } function usr_del_file($usrfile) { if(is_file($usrfile)) { @unlink($usrfile); } } Link to comment https://forums.phpfreaks.com/topic/161842-search-and-delete-a-file-in-a-subdirectory/ Share on other sites More sharing options...
will35010 Posted June 11, 2009 Share Posted June 11, 2009 You could put it in a loop, but you'd have to know every directory's name. Link to comment https://forums.phpfreaks.com/topic/161842-search-and-delete-a-file-in-a-subdirectory/#findComment-853888 Share on other sites More sharing options...
RussellReal Posted June 11, 2009 Share Posted June 11, 2009 will, not exactly.. but you'd need not JUST a LOOP, you'd need a recursive function which loops a single directory and everytime it encounters another directory, begin that loop on the other directories.. its a lil advanced but you could get it right if you try hard enough Link to comment https://forums.phpfreaks.com/topic/161842-search-and-delete-a-file-in-a-subdirectory/#findComment-853899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.