/**
* 判断字符串是否以什么结尾
* @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
相关推荐
matlab代码19 分钟前
基于matlab的水果识别系统(苹果香蕉菠萝梨桃子)【源码65期】wuyk55544 分钟前
从零吃透 MQTT 通信|第 8 章 FreeRTOS 多任务架构下 MQTT 工程架构,任务拆分、队列解耦、临界区保护weixin199701080161 小时前
[特殊字符]《跨境二手ERP对接Back Market:标准化API + 7~10工作日技术合规审核实录》(附Python源码)尘客-追梦1 小时前
Day 01:插件化之前,先看清 ABI 这堵墙持敬chijing1 小时前
Python-数据类型-列表早睡早起身体好1232 小时前
用 XGrammar 约束大模型工具调用:解决参数为空的问题CRMEB系统商城2 小时前
CRMEB标准版系统(Java)v3.1正式发布写后端的胖头鱼3 小时前
【高频面试题】Java 基础类型 + 包装类 + String 互相转换matlab代码3 小时前
基于matlab自助水果超市水果识别收费系统(GUI界面)【源码67期】hui-梦苑3 小时前
[PHP]轻量主题函数WordPress 集成 KaTeX 数学公式渲染