How to Delete Directory and Files Using PHP?

Find best Web Development tips and Tricks.
Post Reply
arnlweb
Site Admin
Posts: 95
Joined: Wed Jun 22, 2016 1:28 pm
Location: India
Contact:

How to Delete Directory and Files Using PHP?

Post by arnlweb » Sun Jan 07, 2018 11:28 am

Delete Directory and Files in a Directory Using PHP


Make a file paste the code and run it.

Code: Select all

<?php

delete_files('/home/admin/web/website.com/public_html/wp-content/cache/comet-cache/cache/');

function delete_files($target) {
    if(is_dir($target)){
        $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
        
        foreach( $files as $file )
        {
            delete_files( $file );      
        }
      
        rmdir( $target );
    } elseif(is_file($target)) {
        unlink( $target );  
    }
}
?>
It works, I tested it.

Post Reply