<?PHP/*Custom Delete functions that can delete files and recursively delete folders*/ functionMy_del ($path)//to customize the My_del function, the function has a parameter ($path). When calling the My_del () function, we also pass a path (e.g. ' PHP '){ if(Is_dir($path))//returns True if the file name exists and is a directory. If file is a relative path, its relative path is checked according to the current working directory. { $file _list=Scandir($path);//the Scandir () function returns an array that contains the files and directories in the specified path. If successful, returns an array, or false if it fails. foreach($file _list as $file)//iterate through each element in the array and loop through the code block { if($file! = '. ' &&$file! = ' ... ')//if the traversed path is not equal to. And the path is not equal to:{My_del ($path.‘ /‘.$file);//path of the original path Plus/plus traversal } } @rmdir($path);//rmdir () function to delete empty directories } Else { @unlink($path);//unlink () function Delete file } } $path= ' php ';//folder path to delete//If the PHP file is not ANSI, but UTF-8 mode, and the folder to be deleted contains Chinese characters, the calling function needs to transcode//$path =iconv (' utf-8 ', ' gb2312 ', $path);My_del ($path);//returns the My_del function, and now all directories become emptyEcho"Initialization complete!!! ";/*What did Scandir get? <?phpprint_r (Scandir ("Images"));? > Output: Array ([0] = =. [ 1] = =. [2] = dog.jpg[3] = house.jpg[4] = logo.gif) So images/dog.jpg is its path.*/
PHP Delete Directory