PHP获取或删除某文件目录的文件名称

<?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');
/*************************************************/
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值