


PHP unlink and rmdir delete directory and all files in the directory_PHP tutorial
Deleting files and directories in PHP is actually very simple, as long as there are two functions, one is unlink and the other is rmdir function. If we want to delete directories and files under directories, we need to use recursion.
Function code: Only delete files in the specified directory, not the directory folder.
The code is as follows
|
Copy code
|
||||||||
//Loop all files in the directory
| while ( false !== ( $item = readdir( $handle ) ) ) {
n";}}}closedir( $handle );}}}?>Suppose you need to delete all files in a directory named "upload" (but do not need to delete the directory folder), you can do the following Code completion: delFileUnderDir( 'upload');<🎜>?> php delete all directories
The code is as follows | Copy code | ||
function deltree($pathdir) { echo $pathdir;//Used for debuggingif(is_empty_dir($pathdir))//If it is empty{ rmdir($pathdir);//Delete directly } else {//Otherwise read this directory, except . and ..$d=dir($pathdir ); while($a=$d->read()) { if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..')){unlink($pathdir.'/'.$a);} //If it is a file, delete it directlyif(is_dir($pathdir.'/ '.$a) && ($a!='.') && ($a!='..')) {//If it is a directoryif(!is_empty_dir($pathdir.'/' .$a))//Whether it is empty{//If not, call itself, which is just the original path + its subordinate directory namedeltree($pathdir.'/'.$a); } if(is_empty_dir($pathdir.'/'.$a)) {//If it is empty, delete it directly rmdir($pathdir.'/'.$a); } } } $d->close(); echo "All files in the directory must be deleted first";//What I use when debugging } } function is_empty_dir($pathdir) { //Determine whether the directory is empty $d=opendir($pathdir); $i=0; while ($a=readdir($d)) { $i++; } closedir($d); if($i>2){return false;} else return true; } PHP deletes the directory and all files in the directory
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
