Logo

Office Address

123/A, Miranda City Likaoli
Prikano, Dope

Phone Number

+0989 7876 9865 9

+(090) 8765 86543 85

Email Address

info@example.com

example.mail@hum.com

Delete ALL Files and Subfolders from Folder in PHP

Delete ALL Files and Subfolders from Folder in PHP

How to delete all files and sub-folders from a folder using php. Like deleting all files of a user without activity for long time, deleting files older than 3 months etc. Whatever the reason, doing such clean-up manually is a tedious task. You can accomplish it with the help of an automated php script. And we are going to explain how to delete directory and all files and sub directories from it with php.

PHP Predefined function to delete the files from the folder.

glob() and unlink() only two native php functions.

  • Function glob() returns an array of filenames/directories matching the given pattern.
  • Function unlink() deletes a given file.

Here is the simple php function I have written to delete all files from a directory. Next we loop through the files one by one and check if the item is a file and not a sub folder itself using is_file() function.

 foreach(glob($dir . '/*') as $file){
        // check if is a file and not sub-directory
        if(is_file($file)){
            // delete file
            unlink($file);
        }
    }

Delete Only Particular File Type from Folder:

Delete only Particular file type for Eg:pdf, Jpg. with the file extension name user can delete only particular file type from the folder. 

foreach(glob($dir . '/*.pdf') as $file){
        // check if is a file and not sub-directory
        if(is_file($file)){
            // delete file
            unlink($file);
        }
    }

Delete Files Older than X Days from Folder:

Deleteing old and unwanted files by setting CRON job user can delete the files older then 90days.

foreach(glob($dir . '/*') as $file){
        if(is_file($file)){
            // check if file older than 90 days
            if((time() - filemtime($file)) > (60 * 60 * 24 * 90))
                unlink($file);
        }
    }


Delete All Files and Sub Folders from a Folder in PHP:

Simply deleting a folder with rmdir() won't work. it'll throw exception if you plan to remove folder with files in it. So first you've got to delete files one by one from each sub folder plus the remainder of the files then delete folders by removing parent folder.

foreach(glob($dir . '/*') as $file) {
        if(is_dir($file))
            deleteAll($file);
        else
            unlink($file);
    }
    rmdir($dir);

Tags

  • PHP delete all files in directory recursive

  • delete all images from folder in PHP

  • delete all files in a directory Laravel

  • unlink multiple files PHP
  • PHP find files in directory and subdirectories
  • CodeIgniter delete all files in directory

  • how to delete files in a folder using PHP

  • PHP script to delete files on server

Related Posts

CodeIgniter upload multiple images using drag and drop using dropzone

CodeIgniter upload multiple images using drag and drop using dropzone

In this example we are going to explain how to upload mulitple images using dropzone library in Codeigniter. Simple drag and drop image on the webpage. dropzone jQuery plugin, you can easily upload im

Read More
How a Professional Web Development Company in Tamil Nadu Can Help Your Brand Stand Out

How a Professional Web Development Company in Tamil Nadu Can Help Your Brand Stand Out

A solid online presence is crucial for brands looking to expand and compete in the digital age. Working with a seasoned web development firm can give businesses in Tamil Nadu, where digital entreprene

Read More
Web Portal Design Company

Web Portal Design Company

Creative, Innovative, and Business-Centric Solutions Businesses are continuously searching for methods to connect people, engage customers, and streamline operations in the fast-paced digital world

Read More