whelpton Posted March 14, 2009 Share Posted March 14, 2009 Hello, I am trying to delete a number of folders using php, after searching the manual I have found the UNSET function, however when I use this it outputs the following error: Warning: unlink(/home/alport/public_html/test) [function.unlink]: Is a directory in /home/alport/public_html/shared/bandedit/delete.php on line 23 The folder is not empty, if that has anything to do with it. I was wondering where am I going wrong? The code I am using with the unlink function is: unlink("/home/alport/public_html/".$_SESSION['s_username'].""); Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2009 Share Posted March 14, 2009 You need a trailing slash / on a directory and it needs to be empty before you can delete it. Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/#findComment-784367 Share on other sites More sharing options...
whelpton Posted March 14, 2009 Author Share Posted March 14, 2009 So there is no way of simply deleting the folder whilst it is full? Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/#findComment-784368 Share on other sites More sharing options...
Festy Posted March 14, 2009 Share Posted March 14, 2009 So there is no way of simply deleting the folder whilst it is full? No there's no way. But the code to empty a folder isn't difficult in any way. Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/#findComment-784369 Share on other sites More sharing options...
whelpton Posted March 14, 2009 Author Share Posted March 14, 2009 No there's no way. But the code to empty a folder isn't difficult in any way. Can you point me in the right direction please Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/#findComment-784370 Share on other sites More sharing options...
Festy Posted March 14, 2009 Share Posted March 14, 2009 No there's no way. But the code to empty a folder isn't difficult in any way. Can you point me in the right direction please Ok, here's the code that lets you delete the directory which has contents. It uses a spl class DirectoryIterator() to iterate through the contents in the directory. Please let me know whether it works or not. <?php $path = '/path_to_your_dir'; if (file_exists(dirname($path))) { foreach (new DirectoryIterator(dirname($path)) as $file) { if (true === $file->isFile()) { unlink($file->getPathName()); } } rmdir(dirname($path)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/#findComment-784377 Share on other sites More sharing options...
whelpton Posted March 14, 2009 Author Share Posted March 14, 2009 Thanks, worked perfectly for me Quote Link to comment https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/#findComment-784387 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.