/**
* 判断字符串是否以什么结尾
* @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
相关推荐
aqiu11111128 分钟前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)爱吃香菜的初学者35 分钟前
十四.Linux——消息队列迅猛龙办公室1 小时前
python实现倒序输出数字夏幻灵7 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱程序猿编码7 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码aqi008 小时前
鸿蒙版本的电子书阅读APP开放源码啦墨天梦8 小时前
B19_SunnyWeather到阅读客户端bkspiderx8 小时前
Qt 插件机制:动态扩展应用功能的核心框架程序员老陆9 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式沐晓时光9 小时前
C语言入门,深入理解指针(3)