<?php
/**
* 获取文件的后缀名称,并且全部化成成小写字母返回
* @param string $fileName 目标文件名称
* @return string 文件后缀名称
*/
function getFileExt($fileName)
{
return strtolower(trim(substr(strrchr($fileName, '.'), 1)));
}
/**
* 列出目录下所有满足条件的文件名
* @param string $path 目录的地址
* @param string $exts 后缀名称,默认:空
* @return array
* @author 李小刚 858864436@qq.com
*/
function getAllFileName($path, $exts='' )
{
$list= array();
global $list;
if(!is_dir($path)) $path = dirname($path);
$dh = opendir($path); //打开文件目录句柄
while(($file = readdir($dh)) !== FALSE) //返回目录中下一个文件的文件名
{
if('.' != $file && '..' != $file)
{
$tempPath = $path. '/'.$file;
if(is_dir($tempPath))
getAllFileName($tempPath, $exts); //递归
else
if (!$exts || $exts == getFileExt($file)) $list[] = $file;
}
}
closedir($dh); //关闭文件目录句柄
return $list;
}
/**
* 删除目录下所有满足条件的文件名
* @param string $path 目录的地址
* @param string $exts 后缀名称,默认:空
* @return boolean
* @author 李小刚 858864436@qq.com
*/
function delAllFile($path, $exts='')
{
if(!is_dir($path)) $path = dirname($path);
$dh = opendir($path); //打开文件目录句柄
while(($file = readdir($dh)) !== FALSE) //返回目录中下一个文件的文件名
{
if('.' != $file && '..' != $file)
{
$tempPath = $path. '/'.$file;
if(is_dir($tempPath))
delAllFile($tempPath, $exts); //递归
else
if (!$exts || $exts == getFileExt($file)) unlink($tempPath);
}
}
closedir($dh); //关闭文件目录句柄
return TRUE;
}
/************************测试数据******************/
header("Content-type:text/html; charset=utf-8");
$path = "d:/test/";
echo '<pre>';
print_r(getAllFileName($path));
echo '</pre>';
delAllFile($path, 'php');
/*************************************************/
?>