php数据库遍历删除文件 php批量删除数据库的原理

php 删除1天以前的文件如果目录不含有其他子目录的话,那就遍历该目录 , 逐个删除
----------------------------------------------------
$dir= '你要删除其内容但不删除该目录的目录地址';
//scandir将以数组的形式返回该目录下所有的文件
$files = scandir($dir);
foreach ($files as $filename){
$thisfile=$dir.'/'.$filename;
if($thisfile!='.'$thisfile!='..'(time()-filemtime($thisfile)) 3600*24)) {
unlink($thisfile);//删除此次遍历到的文件
}
}
------------------------------------
如果你有 php手册 ,你可以直接查询 dir 函数和file 函数
你想要的操作 都有讲到 。
-----------------------------------
php 目录遍历、删除 函数的使用介绍小编今天没事写了目录想关的函数
包括
遍历该文件夹下的文件,目录子目录
读取当前文件下目录和文件
删除当前文件夹下的目录子目录以及文件
以上三个函数目前还不支持中文文件
中文目录
复制代码
代码如下:
?php
header("Content-type:text/html;charset=utf-8");
/**
*
读取当前目录下的文件和目录
*
*
@param
string
$path
路径
*
@return
array
所有满足条件的文件
*/
function
tlist($path){
$path
=
iconv('utf-8',
'gbk',
$path);
if(!is_dir($path)){
throw
new
Exception($path."不是目录");
}
$arr
=
array('dir'=array(),'file'=array());
$hd
=
opendir($path);
while(($file
=
readdir($hd))!==false){
if($file=="."||$file=="..")
{continue;}
if(is_dir($path."/".$file)){
$arr['dir'][]
=
iconv('gbk','utf-8',$file);
}else
if(is_file($path."/".$file)){
$arr['file'][]
=
iconv('gbk','utf-8',$file);
}
}
closedir($hd);
echo
"目录有:".implode("br
/",$arr['dir'])."br
/";
echo
"文件有:".implode("br
/",$arr['file']);
}
/**
*
遍历当前目录下的文件和目录以及子文件夹中目录
*
*
@param
string
$path
路径
*
@return
array
所有满足条件的文件
*/
function
blist($path){
if(!is_dir(iconv("utf-8","gbk",$path))){
throw
new
Exception("文件夹".$path."不存在或者不是文件");
}
$arr
=
array();
$hd
=
opendir(iconv("utf-8","gbk",$path));
while(($file
=
readdir($hd))!==false){
if($file=="."||$file=="..")
{continue;}
$newpath=iconv('utf-8',
'gbk',
$path)
.'/'.$file;
if(is_dir($newpath)){
$arr[]
=
blist($path."/".$file);
}else
if(is_file($newpath)){
$arr[]
=
iconv('gbk','utf-8',$file);
}
}
closedir($hd);
【php数据库遍历删除文件 php批量删除数据库的原理】return
$arr;
}
/**
*
删除目录下的文件以及子目录
*
#param
string
$path
路径
*
#return
string
删除成功返回true
失败返回false;
*/
function
dirDel($path){
if(!is_dir($path)){
throw
new
Exception($path."输入的不是有效目录");
}
$hand
=
opendir($path);
while(($file
=
readdir($hand))!==false){
if($file=="."||$file=="..")
continue;
if(is_dir($path."/".$file)){
dirDel($path."/".$file);
}else{
@unlink($path."/".$file);
}
}
closedir($hand);

推荐阅读