Recursively deleting a directory with files in PHP.

I often face the task of deleting a folder with all its contents, I'm tired of constantly reinventing the wheel, I found a great feature here PHP, which recursively runs through all folders, deleting files and after that the directory.

Here is the actual function:

 

I will describe a little the logic of this function. The function receives the path to the folder as a parameter. The first thing she does is check, does this folder exist and is this folder at all if the result of the check is positive, then it opens the folder for reading and the folder reading cycle starts, get all its contents (folders, files, links) then don't forget to exclude folders '.'(point) and '..'(two points) the first is a link to this directory, the second is a link to the catalog one level higher. After that, we change the access rights to the file or directory, and if it is a folder, then go into it and delete everything in it, otherwise it is a file or a link and then we just delete it.

 

Function usage example:

Article source: https://slawutich.pp.ua/php/58-removedir-php.html

Купить хостинг WordPress
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal