read())){..}" method All files in the directory; finally call the rmdirs method to delete them."> How to delete multi-level directories in php-PHP Problem-php.cn

How to delete multi-level directories in php

藏色散人
Release: 2023-03-05 20:30:01
Original
2517 people have browsed it

php method to delete multi-level directories: first create a PHP sample file; then define a rmdirs method; then pass "while (false!==($child=$d->read())) {..}" method to clear all files in the directory; finally call the rmdirs method to delete.

How to delete multi-level directories in php

Recommendation: "PHP Video Tutorial"

Previously, due to project requirements, it was necessary to use php to create a multi-level directory, then Once created, it must be deleted. We all know that the functions that come with the PHP system include functions for deleting directories, such as rmdir(). The syntax prototype of this function is such as: rmdir(dir,context), which means trying to delete the directory specified by dir. Directory.

But the directory dir must be empty. There is a problem. When there is content in the directory or there are directories in the directory, when we determine that the directory and all the files and directories in the directory have no meaning, we need to delete the directory. How to do it?

The simple rmdir() function can no longer meet our needs, so the following function was born. The following function will directly delete the directory you need to specify to delete. There are Files or multi-level directories will be killed mercilessly by it. Haha, just go to the code.

function rmdirs($dir){ $d=dir($dir); while (false!==($child=$d->read())){//这部分代码是清除目录里面的所有文件的 if($child!='.'&&$child!='..'){ if(is_dir($dir.'/'.$child)){ rmdirs($dir.'/'.$child); }else{ unlink($dir.'/'.$child); } } } $d->close(); rmdir($dir);//清除目录 }
Copy after login

Articles you may be interested in

The above is the detailed content of How to delete multi-level directories in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!