CancelImage Upload

How to delete or remove a folder from a Server using PHP

While deleting a folder is no more difficult than deleting a file (though requiring a different set of functions), there are a few things that need to be taken into account. For starters that the file will not be removable until you have deleted each of it's files one by one. To accomplish this you will need to use a combination of the function readdir() to load all the filenames within a folder, and then one by one deleting them with the file delete function that I have previously discussed.

Now then, moving back to our original topic. If you have read my file delete tutorial the same principles apply for folders. The only difference being that the function file_exists() will be replaced by is_dir() to check whether or not the directory is indeed real, and instead of unlink() we shall use rmdir() to delete the folder.

Here then is the code to accomplish this task with the above discussed functions. As you will see it is nothing complex:

<?php

        $directory = "folder";

        if(is_dir($directory))
        {

                if(rmdir($directory))
                {

                        echo "The folder <i>".$directory."</i> has been deleted.";

                }
                else
                {

                        echo "The folder <i>".$directory."</i> could not be deleted.";

                }
        }
        else
        {

                echo "The folder <i>".$directory."</i> could not be found.";

        }

?>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com