The Little Guy Posted December 14, 2007 Share Posted December 14, 2007 How can I choose a root directory such as dir/dir2 and delete all of it's files, folders, sub files, and sub folders? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 14, 2007 Share Posted December 14, 2007 http://ca.php.net/rm_dir That should do the trick. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 14, 2007 Author Share Posted December 14, 2007 That only works on empty directories... But I think I just thought of a way to do this.... Let me give it a try. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 14, 2007 Share Posted December 14, 2007 If you're using *nix you could just exec it to the shell: $dir = '/the/dir/to/delete'; exec('rm -r ' . $dir); Quote Link to comment Share on other sites More sharing options...
corbin Posted December 14, 2007 Share Posted December 14, 2007 This is taken off of the documentation page for unlink (http://php.net/unlink). james at NOSPAM dot telserco dot com 29-Sep-2007 10:10 Cheap and dirty example for cleaning a directory of files so many seconds old. function cleantmp() { $seconds_old = 3600; $directory = "/var/tmp"; if( !$dirhandle = @opendir($directory) ) return; while( false !== ($filename = readdir($dirhandle)) ) { if( $filename != "." && $filename != ".." ) { $filename = $directory. "/". $filename; if( @filemtime($filename) < (time()-$seconds_old) ) @unlink($filename); } } } Some modding, and that should fit your needs. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 14, 2007 Author Share Posted December 14, 2007 Here is what I came up with: Good or not? <?php if(isset($_GET['dir'])){ $dirs = $_GET['dir']."%"; $query = sprintf("SELECT * FROM albums WHERE owner = '%s' AND album LIKE '%s' ORDER by album DESC", mysql_real_escape_string($_SESSION['id']), mysql_real_escape_string($dirs)); $sql = mysql_query($query); while($row = mysql_fetch_array($sql)){ $sq = mysql_query("SELECT * FROM files WHERE directory = '{$row['album']}' AND owner_id = '{$_SESSION['id']}'"); while($ro = mysql_fetch_array($sq)){ if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos/'.$ro['file_name'])){ unlink('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos/'.$ro['file_name']); } if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs/'.$ro['file_name'])){ unlink('../../tzfiles.com/users/'.$row['album'].'/thumbs/'.$ro['file_name']); } unlink('../../tzfiles.com/users/'.$row['album'].'/'.$ro['file_name']); } if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos')){ rmdir('../../tzfiles.com/users/'.$row['album'].'/thumbs/videos'); } if(file_exists('../../tzfiles.com/users/'.$row['album'].'/thumbs')){ rmdir('../../tzfiles.com/users/'.$row['album'].'/thumbs'); } rmdir('../../tzfiles.com/users/'.$row['album']); mysql_query("DELETE FROM files WHERE directory = '{$row['album']}' AND owner_id = '{$_SESSION['id']}'"); mysql_query("DELETE FROM albums WHERE album = '{$row['album']}' AND owner = '{$_SESSION['id']}'")or die(mysql_error()); } } ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 14, 2007 Author Share Posted December 14, 2007 Well... Crap... I just deleted 100 files in < 1 second, with out any errors, so looks like it works fine. I just need to recode some of it... so it doesn't do that again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.