PHP函数rmdir()的使用技巧讲解
在PHP语言中,有许多函数供我们编程使用,同样他们的功能也是非常强大的。我们今天就要为大家介绍的有关PHP 是一种嵌入在 HTML 并由服务器解释的脚本语言。它可以用于管理动态内容、支持数据库、处理会话跟踪,甚至构建整个电子商务站点。它支持许多流行的数据库,包括 MySQL、PostgreSQL、Oracle、Sybase、Informix 和 Microsoft SQL Server。动态内容为什 么这么热门?假设您正在管理有 10 个产品的电子商务站点。只要产品不是经常变动或者预料到它不会有太大的变动,那么手工编写 10 个带有必要的信息、表单和诸如此类内容的静态产品页面是不困难的。但是,假设您在本月再要添加 10 个或更多产品,然后在下个月要更多,而且价格有时会变动或者想改变站点的观感。那么您就会陷入用手工重新编写数十个,也许上百个静态页面的困境中。
另一方面,假设您从创建 product.php 页面开始。它没有静态信息,而是编码成可以从产品数据库中提取信息并动态地构建一个页面。然后您就拥有了一个元数据页面,它可以根据存储在数据库中的信息 提供一个、一百个、甚至十万个单独页面。现在网站管理员不再整天都简单重复更新静态页面的工作,因为在更新公司数据库中的信息同时就可以更新页面上的信 息。这样就消除了令人头疼的时间延迟(在数据库中更改信息和在网站上显示信息之间的时间间隔)。下面我们来看一个PHP递归删除目录的例子,希望对大家有帮助。
PHP函数rmdir()就可以搞定,但是要删除一个非空目录,将不能进行快速的删除,必须先将目录中文件删除,但是目录里可能还会有子目录所以要进行PHP递归删除目录:
PHP递归删除目录代码:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>functiondeletedir($dir){ </span></li> <li class="alt"> <span>if(!</span><span class="attribute">handle</span><span>=@opendir($dir)){//检测要打开目录是否存在 </span> </li> <li><span>die("没有该目录"); </span></li> <li class="alt"><span>} </span></li> <li> <span>while(false!==($</span><span class="attribute">file</span><span>=</span><span class="attribute-value">readdir</span><span>($handle))){ </span> </li> <li class="alt"><span>if($file!=="."&&$file!==".."){//排除当前目录与父级目录 </span></li> <li> <span>$</span><span class="attribute">file</span><span>=$dir.DIRECTORY_SEPARATOR.$file; </span> </li> <li class="alt"><span>if(is_dir($file)){ </span></li> <li><span>deletedir($file); </span></li> <li class="alt"><span>}else{ </span></li> <li><span>if(@unlink($file)){ </span></li> <li class="alt"> <span>echo"文件</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$file</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除成功。</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span></span> </li> <li><span>}else{ </span></li> <li class="alt"> <span>echo"文件</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$file</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除失败!</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span></span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>if(@rmdir($dir)){ </span></li> <li> <span>echo"目录</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$dir</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除成功了。</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>n"; </span></span></span> </li> <li class="alt"><span>}else{ </span></li> <li> <span>echo"目录</span><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span>$dir</span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span><span>删除失败!</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>n"; </span></span></span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>//测试程序 </span></li> <li> <span>$</span><span class="attribute">dir</span><span>=</span><span class="attribute-value">"/var/www/test"</span><span>; </span> </li> <li class="alt"><span>deletedir($dir); </span></li> <li> <span>?</span><span class="tag">></span><span> </span> </li> </ol>
在/var/www/test文件夹下建一写文件夹和文件测试
<ol class="dp-xml"> <li class="alt"><span><span>shell</span><span class="tag">></span><span>touchaaa </span></span></li> <li> <span>shell</span><span class="tag">></span><span>touchbbb </span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>touchccc </span> </li> <li> <span>shell</span><span class="tag">></span><span>toucheee </span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>touchffff </span> </li> <li> <span>shell</span><span class="tag">></span><span>mkdir111 </span> </li> <li class="alt"> <span>shell</span><span class="tag">></span><span>mkdir222 </span> </li> <li> <span>shell</span><span class="tag">></span><span>mkdir333 </span> </li> </ol>
分别再在111,222,333文件夹下建写文件这里就不多说了,然后给他们权限
<ol class="dp-xml"><li class="alt"><span><span>shell</span><span class="tag">></span><span>chown[url]www.www[/url]test-R </span></span></li></ol>
以上就是PHP函数rmdir()实现PHP递归删除目录的具体方式和测试结果,供大家参考。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid
