How to delete folders in PHP _ PHP Tutorial

Source: Internet
Author: User
Explore three methods for deleting folders in PHP. 1. copy the code using recursion: deleteDir ($ dir) {if (rmdir ($ dir) falseis_dir ($ dir) {if ($ dpopendir ($ dir )) {while ($ filereaddir ($ dp ))! False) {if (is_dir ($ fil 1. recursion

The code is as follows:


DeleteDir ($ dir)
{
If (rmdir ($ dir) = false & is_dir ($ dir )){
If ($ dp = opendir ($ dir )){
While ($ file = readdir ($ dp ))! = False ){
If (is_dir ($ file) & $ file! = '.' & $ File! = '..'){
DeleteDir ($ file );
} Else {
Unlink ($ file );
}
}
Closedir ($ dp );
} Else {
Exit ('not permission ');
}
}
}


2. system call method

The code is as follows:


Function del_dir ($ dir)
{
If (strtoupper (substr (PHP_ OS, 0, 3) = 'win '){
$ Str = "rmdir/s/q". $ dir;
} Else {
$ Str = "rm-Rf". $ dir;
}
}


3. circulation method

The code is as follows:


Function deltree ($ pathdir)
{
Echo $ pathdir; //
If (is_empty_dir ($ pathdir) // if it is empty
{
Rmdir ($ pathdir); // delete directly
}
Else
{// Otherwise, read this directory, except
$ D = dir ($ pathdir );
While ($ a = $ d-> read ())
{
If (is_file ($ pathdir. '/'. $ a) & ($! = '.') & ($! = '..') {Unlink ($ pathdir. '/'. $ );}
// Delete a file directly
If (is_dir ($ pathdir. '/'. $ a) & ($! = '.') & ($! = '..'))
{// If it is a directory
If (! Is_empty_dir ($ pathdir. '/'. $ a) // whether it is null
{// If not, call itself, but the original path + its sub-directory name
Deltree ($ pathdir. '/'. $ );
}
If (is_empty_dir ($ pathdir. '/'. $ ))
{// Delete directly if it is null
Rmdir ($ pathdir. '/'. $ );
}
}
}
$ D-> close ();
Echo "all files in the directory must be deleted first"; //
}
}
Function is_empty_dir ($ pathdir)
{
// Check whether the directory is empty. isn't my method good? It's just that there are other things except... and not empty.
$ D = opendir ($ pathdir );
$ I = 0;
While ($ a = readdir ($ d ))
{
$ I ++;
}
Closedir ($ d );
If ($ I> 2) {return false ;}
Else return true;
}


Example code: deleteDir ($ dir) {if (rmdir ($ dir) = false is_dir ($ dir) {if ($ dp = opendir ($ dir )) {while ($ file = readdir ($ dp ))! = False) {if (is_dir ($ fil...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.