/**
* 判断字符串是否以什么结尾
* @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
相关推荐
ServBay10 分钟前
WordPress 核心漏洞 wp2shell,波及超 5 亿网站Ulyanov1 小时前
Python实现6-DOF刚体仿真器(下)——环境扰动与控制闭环分布式存储与RustFS1 小时前
RustFS Beta.10 性能解读:PUT 全面反超 MinIO,Rust 重写对象存储成了?小小的木头人1 小时前
Python 批量解析 Excel 经纬度,调用高德地图 API 获取中文地址nzz_1712142 小时前
PHP程序员转型AI岗位指南:核心技能、北京就业市场与转型路径分析Risehuxyc2 小时前
C# 将doc转换为docx阿pin3 小时前
Android随笔-SharedPreferences多加点辣也没关系3 小时前
JavaScript|第24章:事件循环与并发模型ん贤4 小时前
怎么设计,才算一个优秀审计模块l1t4 小时前
测试用rust重写的postgresql: pgrust