/**
* 判断字符串是否以什么结尾
* @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
相关推荐
武当豆豆43 分钟前
C++编程学习阶段性总结学不动CV了1 小时前
C语言32个关键字你怎么知道我是队长2 小时前
python-enumrate函数小屁孩大帅-杨一凡2 小时前
如何解决ThreadLocal内存泄漏问题?大熋2 小时前
Playwright Python 教程:网页自动化还是奇怪2 小时前
Linux - 安全排查 3赟赟、嵌入式2 小时前
imx6ul Qt运行qml报错This plugin does not support createPlatformOpenGLContext!Android采码蜂3 小时前
BLASTBufferQueue03-BufferQueueConsumer核心操作Android采码蜂3 小时前
BLASTBufferQueue02-BufferQueueProducer核心操作cdg==吃蛋糕3 小时前
selenium 使用方法