bobinindia Posted March 16, 2008 Share Posted March 16, 2008 I am trying to empty a directory of jpeg files with unknown names. Can i use unlink() with a wildcard? If so how? Link to comment https://forums.phpfreaks.com/topic/96424-empty-but-not-delete-a-directory/ Share on other sites More sharing options...
Orio Posted March 16, 2008 Share Posted March 16, 2008 <?php //$dir = folder name to empty, with an ending "/" function empty_dir ($dir) { $files = glob($dir."*"); foreach($files as $file) { if(is_dir($file)) { if($file != "." && $file != "..") empty_dir($dir.$file."/"); } else unlink($file); } } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/96424-empty-but-not-delete-a-directory/#findComment-493511 Share on other sites More sharing options...
bobinindia Posted March 16, 2008 Author Share Posted March 16, 2008 i have tried your code like this and the folder has the correct permissions. Any other suggestions? <?php function empty_dir ($dir) { $files = glob("*"); foreach($files as $file) { if(is_dir($file)) { if($file != "." && $file != "..") empty_dir($dir.$file."/"); } else unlink($file); } } $dir = "images/changer/"; empty_dir($dir); ?> Link to comment https://forums.phpfreaks.com/topic/96424-empty-but-not-delete-a-directory/#findComment-493533 Share on other sites More sharing options...
Orio Posted March 16, 2008 Share Posted March 16, 2008 I've edited my code 20 seconds after I posted it with a little fix. Copy it again. Orio. Link to comment https://forums.phpfreaks.com/topic/96424-empty-but-not-delete-a-directory/#findComment-493536 Share on other sites More sharing options...
bobinindia Posted March 16, 2008 Author Share Posted March 16, 2008 20 seconds makes all the difference. Thanks. Link to comment https://forums.phpfreaks.com/topic/96424-empty-but-not-delete-a-directory/#findComment-493543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.