当前位置: 首页 > news >正文

php判断某个目录下是否存在文件

/*** 判断字符串是否以什么结尾* @param  String  $haystack 字符串* @param  String  $needle 结尾* @return Boolean*/
function endWith($haystack, $needle) {$length = strlen($needle);if ($length == 0) {return true;}return (substr($haystack, -$length) === $needle);
}
/*** 判断目录下是否存在文件* @param  String  $path 目录路径* @param  String  $recursive 是否递归(false情况下只判断第一级目录,true递归目录下所有文件判断)* @return Boolean*/
function dirExistFile($path,$recursive = false) {if (!is_dir($path)) {return false;}// 目录相隔符号,linux是 /,window一般是\if(!(endWith($path, '/') || endWith($path, '\\'))){$path .= '/';}if($recursive){// 递归遍历目录$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path),\RecursiveIteratorIterator::LEAVES_ONLY);$isExistFile = false;foreach ($files as $file) {if ($file->isFile()) {$isExistFile = true;break;}}return $isExistFile;}else{$files = scandir($path);// 删除  "." 和 ".."unset($files[0]);unset($files[1]);$isExistFile = false;foreach ($files as $fileName) {$fileNamePath = $path.$fileName;if(is_file($fileNamePath)){$isExistFile = true;break;}}return $isExistFile;}
}var_dump(dirExistFile('C:\test',true));

http://www.lryc.cn/news/410188.html

相关文章:

  • 重塑互联网生态:探索Web 3.0、大数据与隐私保护的新篇章
  • HR模块中PA信息类型的相关函数
  • c# 日期类型变量默认值
  • 设计模式实战:任务调度系统的设计与实现
  • 代码中的特殊注释
  • ubuntu20.04.6 安装Skywalking 10.0.1
  • C++:map和set
  • 深入理解二叉搜索树:定义、操作及平衡二叉树
  • vue3组件通信(二)
  • 关键词查找【Boyer-Moore 算法】
  • 【前端手写代码】手写Object.create
  • 速通JS模块化规范
  • HamonyOS性能优化工具和方法
  • 前端实现边下载文件边上传
  • 滑线变阻器的优缺点是什么?
  • K8s大模型算力调度策略的深度解析
  • Unity Transform组件实现动画:基础与进阶技巧
  • 基于深度学习的图像与文本结合
  • windows安全加固
  • 网络安全是什么?怎么入门网络安全?
  • 语义分割介绍
  • Unity Editor免登录启动 无需UnityHub
  • Redis实战篇(黑马点评)笔记总结
  • vulntarget-b
  • Axure Web端元件库:构建高效互动网页的基石
  • mac OS matplotlib missing from font(s) DejaVu Sans
  • 在 .NET 中使用 Elasticsearch:从安装到实现搜索功能的完整指南
  • Ecovadis认证的步骤需要怎么做?
  • git sendemail使用
  • 【React】package.json 文件详解