Home > Article > Backend Development > PHP deletes a file/folder in a specified directory - How to delete a specified file/folder in a directory using PHP? _PHP Tutorial
1
2 $dir = 'The directory path you want to delete'; //As follows:
3 //$dir = $_SERVER['DOCUMENT_ROOT'].'/cache';
4 rmdirs($dir);
5
6 //php deletes files in a specified directory-how to delete a file specified in a directory using PHP?
7 function rmdirs($dir){
8 //error_reporting(0); The function will return a status, I use error_reporting(0) to mask the output
9 //The rmdir function will return a status, I use @ to block the output
10 $dir_arr = scandir($dir);
11 foreach($dir_arr as $key=>$val){
12 if($val == '.' || $val == '..'){}
13 else {
14 if(is_dir($dir.'/'.$val))
15
16 if(@rmdir($dir.'/'.$val) == 'true'){} //Remove @ and take a look17 else
18 rmdirs($dir.'/'.$val);
19 }22
21 unlink($dir.'/'.$val);
22 }
23 }
24 }
25 ?>
PHP deletes folders and files in a specified directory - How to delete a specified folder and file in a directory using PHP?
Author Netzong
http://www.bkjia.com/PHPjc/478536.html
www.bkjia.com