PHP Delete All Directory Files In Directory

Today, We want to share with you PHP Delete All Directory Files In Directory.In this post we will show you Remove All files older than x days in php, hear for Remove all files, folders and their subfolders with php we will give you demo and example for implement.In this post, we will learn about PHP: Unlink All Files Within A Directory, and then Deleting That Directory with an example.

PHP Delete All Directory Files In Directory

There are the Following The simple About Remove All Directory Files In Directory Full Information With Example and source code.

Read Also:  how to show error messages below the textbox in javascript validation?

As I will cover this Post with live Working example to develop Recursively Remove a Directory in PHP, so the php delete all files and folders in directory for this example is following below.

PHP Function To Remove Directory And Files

<?php
function custom_delete_directory($directory_name) {
         if (is_dir($directory_name))
           $directory_handle = opendir($directory_name);
     if (!$directory_handle)
          return false;
     while($file = readdir($directory_handle)) {
           if ($file != "." && $file != "..") {
                if (!is_dir($directory_name."/".$file))
                     unlink($directory_name."/".$file);
                else
                     custom_delete_directory($directory_name.'/'.$file);
           }
     }
     closedir($directory_handle);
     rmdir($directory_name);
     return true;
}
?>

php remove function that deals with directories recursively

Read Also:  PHP Remove File from Folder and Database

<?php

custom_delete_directory('/path/admin/api/v1/');
function custom_delete_directory($dir_target) {
    if(is_dir($dir_target)){
        $files = glob( $dir_target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned

        foreach( $files as $file ){
            custom_delete_directory( $file );      
        }

        rmdir( $dir_target );
    } elseif(is_file($dir_target)) {
        unlink( $dir_target );  
    }
}
?>

remove all files and sub-directories in a directory

simple PHP Code to recursively remove a directory and its entire contents (files + sub dirs)

<?php 
function deleteAll_dir_files($str) { 
    if (is_file($str)) { 
        return unlink($str); 
    } 
     
    elseif (is_dir($str)) { 
          
        $scan_all = glob(rtrim($str, '/').'/*'); 
          
        foreach($scan_all as $index=>$path) { 
            deleteAll_dir_files($path); 
        } 
          
        return @rmdir($str); 
    } 
} 
   
deleteAll_dir_files('/path/admin/api/v1/');  
  
?> 

PHP: Remove all files from a folder

$folder = '/path/admin/api/v1/';
 
$files = glob($folder . '/*');
foreach($files as $file){
    if(is_file($file)){
        unlink($file);
    }
}

Web Programming Tutorials Example with Demo

Read :

Read Also:  Simple Vuejs Date Filter Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Remove All Directory Files In Directory.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Get Folder Path From File Path Examples
  2. Read Also:  PHP Laravel delete files from Public Folder
  3. Read Also:  How to count files in a directory using PHP Laravel?
  4. Read Also:  VueJS Trim String - VueJS Trim LTrim and RTrim Functions
  5. Read Also:  Delete All Files from Folder with PHP
  6. Read Also:  Bitcoin Altcoin Ethereum Cryptocurrency Exchange Script
  7. Read Also:  Basic OOP PHP CRUD Operation Using MySQLi from Scratch
  8. Read Also:  PHP Laravel carbon convert String To Date
  9. Read Also:  Bootstrap Forms group - Examples & Tutorials
  10. Read Also:  laravel multi auth (Authentication) guards Example