Home Backend Development PHP Tutorial php清空(删除)指定目录下的文件,不删除目录文件夹的方法

php清空(删除)指定目录下的文件,不删除目录文件夹的方法

Jun 20, 2016 pm 01:02 PM
amp dirname quot

web 开发中我们可能会碰到需要将某个目录下的所有文件清空,但不删除这个目录下的子目录(当然也不会删除被删除的根目录)的情况。

那么对于这种只删除文件而不删除目录的方法,你是如何处理的呢?

下面博主将要和你分享一下对于这个问题的一种比较好的解决办法。

看下面这个函数:

<p>/*删除指定目录下的文件,不删除目录文件夹*/</p>function delFile($dirName){<br />	if(file_exists($dirName) && $handle=opendir($dirName)){<br />		while(false!==($item = readdir($handle))){<br />			if($item!= "." && $item != ".."){<br />				if(file_exists($dirName.'/'.$item) && is_dir($dirName.'/'.$item)){<br />					delFile($dirName.'/'.$item);<br />				}else{<br />					if(unlink($dirName.'/'.$item)){<br />						return true;<br />					}<br />				}<br />			}<br />		}<br />		closedir( $handle);<br />	}<br />}
Copy after login


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What coin is AMP? What coin is AMP? Feb 24, 2024 pm 09:16 PM

What is AMP Coin? The AMP token was created by the Synereo team in 2015 as the main trading currency of the Synereo platform. AMP token aims to provide users with a better digital economic experience through multiple functions and uses. Purpose of AMP Token The AMP Token has multiple roles and functions in the Synereo platform. First, as part of the platform’s cryptocurrency reward system, users are able to earn AMP rewards by sharing and promoting content, a mechanism that encourages users to participate more actively in the platform’s activities. AMP tokens can also be used to promote and distribute content on the Synereo platform. Users can increase the visibility of their content on the platform by using AMP tokens to attract more viewers to view and share

How to use the dirname function in PHP to get the directory part of the path How to use the dirname function in PHP to get the directory part of the path Jun 26, 2023 pm 12:02 PM

PHP is a programming language widely used in web development. It provides many built-in functions to facilitate us to deal with various file path issues during development. For example, the dirname function is one of the commonly used path processing functions in PHP. In this article, we will explore how to use the dirname function in PHP to get the directory portion of the path. The dirname function returns the directory portion of the specified path. The syntax of this function is as follows: dirname($path) where $path is a character

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失怎么解决 图片消失怎么解决 Apr 07, 2024 pm 03:02 PM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

图片消失怎么解决 图片消失怎么解决 Jun 13, 2016 am 10:09 AM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 Jun 13, 2016 am 10:24 AM

为什么我在php上写的这个代码,在浏览器上什么都不显示啊

Amp (AMP) Crypto Hints at Reversal Ahead Amp (AMP) Crypto Hints at Reversal Ahead Aug 27, 2024 am 09:04 AM

Amp [AMP] crypto price was up 11% in the last seven days and was trading at $0.0043, at the time of writing according to CoinMarketCap.

See all articles