Using RmDir () function to implement PHP recursive delete directory _php tutorial

Source: Internet
Author: User
We all know that PHP is a small open source technology, as more and more people realize its practicality and gradually developed. Rasmus Lerdorf released the first version of PHP in 1994. Since then it has developed rapidly and has been developed to version 4.0.3 by numerous improvements and refinements in the original release.

PHP is a scripting language embedded in HTML and interpreted by the server. It can be used to manage dynamic content, support databases, process session tracking, and even build an entire e-commerce site. It supports many popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. Why is dynamic content so hot? Let's say you're managing an ecommerce site with 10 products. It is not difficult to manually write 10 static product pages with the necessary information, forms, and the like, as long as the product is not constantly changing or anticipates that it will not change too much. However, let's say you add 10 or more products this month, and then you want more next month, and the price sometimes changes or you want to change the look and feel of the site. Then you will be caught in the dilemma of manually rewriting dozens of, perhaps hundreds of static pages.

On the other hand, suppose you start by creating a product.php page. It has no static information, but is coded to extract information from the product database and dynamically build a page. Then you have a metadata page that can provide one, 100, or even 100,000 separate pages based on the information stored in the database. Now the site administrator no longer simply repeats the task of updating a static page all day, because the information on the page can be updated at the same time as it is updated in the company database. This eliminates the headaches of time lag (the time interval between changing information in the database and displaying information on the site). Let's take a look at a php recursive delete directory example, I hope to be helpful to everyone.

RmDir () function can be done, but to delete a non-empty directory, you will not be able to quickly delete, you must first delete the directory files, but the directory may also have subdirectories, so you want to do php recursive delete directory:

PHP recursively deletes directory code:

 
 
  1. < ? PHP
  2. Functiondeletedir ($dir) {
  3. if (! Handle = @opendir ($dir)) {//Detect if the directory to open exists
  4. Die ("No such directory");
  5. }
  6. While (false!== ($file=readdir($handle))) {
  7. if ($file!== "." && $file!== "..") {//Exclude current directory from parent directory
  8. $ file = $dir. Directory_separator. $file;
  9. if (Is_dir ($file)) {
  10. Deletedir ($file);
  11. }else{
  12. if (@unlink ($file)) {
  13. echo "File < b > $file b> Delete succeeded. <br>";
  14. }else{
  15. echo "File < b > $file b> Delete failed! < BR > ";
  16. }
  17. }
  18. }
  19. if (@rmdir ($dir)) {
  20. echo "Catalog < b > $dir b> Delete succeeded. <br>n ";
  21. }else{
  22. echo "Catalog < b > $dir b> Delete failed! <br>n ";
  23. }
  24. }
  25. Test program
  26. $ dir = "/var/www/test" ;
  27. Deletedir ($dir);
  28. ? >
  29. Create a Write folder and file test under the/var/www/test folder
  30. Shell > touchaaa
  31. Shell > touchbbb
  32. Shell > TOUCHCCC
  33. Shell > toucheee
  34. Shell > touchffff
  35. Shell > mkdir111
  36. Shell > mkdir222
  37. Shell > mkdir333
  38. Write files in the 111,222,333 folder, respectively. There's not much to say, and then give them permission.
  39. Shell > Chown[url]www.www[/url]test-r

http://www.bkjia.com/PHPjc/446454.html www.bkjia.com true http://www.bkjia.com/PHPjc/446454.html techarticle we all know that PHP is a small open source technology, as more and more people realize its practicality and gradually developed. Rasmus Lerdorf in 1994 released the P ...

  • 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.