How does PHP delete all files in a folder?

Source: Internet
Author: User
Ask for advice. However, you cannot delete the original folder. You can only delete files. But you cannot delete the original folder. You can only delete files.

Reply content:

Ask for advice. But you cannot delete the original folder. You can only delete files.

The above are all linux Command lines. I provide a pure PHP code.
Delete all files in a directory in one sentence

array_map('unlink', glob('*'));

From stew-Research on web
Http://levi.cg.am/archives/3524

/**
* Delete A directory (including the following files)
* @ Return void
*/
Function del_directory ($ directory, $ subdir = true ){
If (is_dir ($ directory) = false ){
Exit ("The Directory Is Not Exist! ");
}
$ Handle = opendir ($ directory );
While ($ file = readdir ($ handle ))! = False ){
If ($ file! = "." & $ File! = ".."){
Is_dir ("$ directory/$ file ")?
Del_directory ("$ directory/$ file "):
Unlink ("$ directory/$ file ");
}
}
If (readdir ($ handle) = false ){
Closedir ($ handle );
Rmdir ($ directory );
}
}

/**
* Delete all files under the directory, but do not delete the Directory
* @ Return void
*/
Function del_directory_file ($ directory ){
If (is_dir ($ directory) = false ){
Exit ("The Directory Is Not Exist! ");
}
$ Handle = opendir ($ directory );
While ($ file = readdir ($ handle ))! = False ){
If ($ file! = "." & $ File! = ".." & Is_file ("$ directory/$ file ")){
Unlink ("$ directory/$ file ");
}
}
Closedir ($ handle );
}

Can I delete the folder and create a folder with the same name (you delete all files anyway, of course, you have to pay attention to the permission )? If not, read the following blog:
Http://cgxcn.blog.163.com/blog/static/13231242200931792027965/

Using more search engines, these questions are easily found on the internet, and there is no need to ask here.


  

Hum, he was also stepped on...
This kind of thing should not have been handed over to PHP... Why is it necessary to use the command line! N mausen

Related Article

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.