Home  >  Article  >  Backend Development  >  The simplest way to delete directories and files in php, _PHP tutorial

The simplest way to delete directories and files in php, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:48707browse

The simplest way to delete directories and files in php,

The example in this article describes the simplest method of deleting directories and files in PHP. Share it with everyone for your reference.

The specific implementation code is as follows:

Copy code The code is as follows:
//Delete all empty directories under the directory
array_map('rmdir', glob('*', GLOB_ONLYDIR));
//Delete all files in the directory
array_map('unlink', array_filter(glob('*'), 'is_file'));
?>

Principle analysis: array_map('rmdir', glob('*', GLOB_ONLYDIR));

Deletion is simply done by using the array_map function. Its function is to return the array after the user-defined function is applied. The number of parameters accepted by the callback function should be consistent with the number of arrays passed to the array_map() function, while glob traverses the directory. Then give the returned array to rmdir to delete the directory, and then:

Copy code The code is as follows:
array_map('unlink', array_filter(glob('*'), 'is_file'));

The principle is almost the same, that is, after traversing the directory, we can delete the files in the specified directory.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/919263.htmlTechArticleThe simplest way to delete directories and files in php. This article describes the simplest way to delete directories and files in php. method. Share it with everyone for your reference. The specific implementation code is as follows...
Statement:
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