/**
* 判断字符串是否以什么结尾
* @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));
php判断某个目录下是否存在文件
Future_object2024-07-30 12:49
相关推荐
方安乐22 分钟前
python之向量、向量和、向量点积Gary Studio1 小时前
安卓HAL编写小小小米粒2 小时前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。czhc11400756633 小时前
C# 428 线程、异步:1213 小时前
java基础SilentSamsara4 小时前
Python 环境搭建完整指南:从下载安装到运行第一个程序小短腿的代码世界4 小时前
Qt文件系统与IO深度解析:从QFile到异步文件操作合天网安实验室4 小时前
记录一个免杀的php webshell demo_李小白4 小时前
【android opencv学习笔记】Day 2: Mat类(图片数据结构体)AnalogElectronic5 小时前
linux 测试网络和端口是否连通的命令详解