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? Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/ 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. Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415129 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. Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415136 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); Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415142 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. Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415149 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()); } } ?> Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415162 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. Link to comment https://forums.phpfreaks.com/topic/81738-deep-file-delete/#findComment-415167 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.