This article describes the method of creating multi-level directories and cascading delete files in PHP. Share it for your reference, as follows:
Create multi-level directory
mkdir
Functions can only create first-level directories. If we want to create multi-level directories, we need to write the functions ourselves.
<?php $path = "one/two/three/four"; function mkdir_p($path,$mode=0700){ $arr = explode("/",$path); $path = ''; foreach($arr as $v){ $path .= $v; mkdir($path,$mode); $path .= "/"; } } mkdir_p($path);
Delete files in cascading
We know PHPrmdir
Functions can only delete empty folders.unlink
Only used to delete files.
We can write functions ourselves and delete non-empty folders cascaded.
<?php $path = $_SERVER['DOCUMENT_ROOT']."lib"; function rmdir_r($path){ $handle = opendir($path); while($file=readdir($handle)){ //Delete all folders $type = filetype($path."/".$file); if($file=='.'||$file=="..") continue; if($type=="file"){ //If the type is file, delete it unlink($path."/".$file); } if($type=="dir"){ //If the type is a folder, then cascadingly delete rmdir_r($path."/".$file); } } closedir($handle); rmdir($path); } rmdir_r($path);
For more information about PHP related content, please check out the topic of this site:PHP file operation summary》、《Summary of PHP directory operation skills》、《Summary of common traversal algorithms and techniques for PHP》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm"and"Summary of PHP network programming skills》
I hope this article will be helpful to everyone's PHP programming.