Jump to content

[SOLVED] deleting folders using php.


whelpton

Recommended Posts

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']."");  

 

 

Link to comment
https://forums.phpfreaks.com/topic/149346-solved-deleting-folders-using-php/
Share on other sites

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));
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.