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

PHP deletes a file/folder in a specified directory - How to delete a specified file/folder in a directory using PHP? _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:15742browse

1

2 $dir = 'The directory path you want to delete'; //As follows:

3 //$dir = $_SERVER['DOCUMENT_ROOT'].'/cache';

4 rmdirs($dir);

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 look

17 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

http: //www.bkjia.com/PHPjc/478536.htmlTechArticle1 ?php 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 the specified directory-how to delete a directory using PHP...
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