Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.