is_dir() 是 PHP 内建的函数之一,用于判断某路径是否为一个存在的目录:
<code> $path = '/var/www/html/uploads'; if (is_dir($path)) { echo "这是一个有效的目录"; } else { echo "该路径不是目录或目录不存在"; } </code>这个函数返回布尔值 true 或 false,依据的是服务器上的真实文件系统状态。
在调用 is_dir() 时尽量使用绝对路径,相对路径可能会因为当前工作目录(CWD)的变化而导致验证失败。例如:
<code> $relativePath = 'images'; $absolutePath = __DIR__ . '/images';if (is_dir($absolutePath)) {
// 推荐方式
}
</code>
realpath() 可以将路径规范化,移除如 ../ 等相对路径成分,同时也可以识别软链接。结合 is_dir() 使用,可以更有效地进行路径验证:
<code> $path = realpath('/var/www/html/uploads'); if ($path !== false && is_dir($path)) { // 确保路径既存在,又是目录 } </code>当从用户输入中获取路径时,务必过滤危险字符,如 ..,避免目录遍历攻击。可以结合正则表达式或白名单机制:
<code> $input = $_GET['dir'] ?? ''; if (preg_match('/^[a-zA-Z0-9_\-\/]+$/', $input)) { $path = realpath('/var/www/html/' . $input); if ($path !== false && is_dir($path)) { // 安全访问 } } </code>虽然 is_dir() 会返回 false 对于不存在的路径,但如果你需要更严谨的检查逻辑,可以先用 file_exists() 做判断:
<code> $path = '/var/www/html/uploads'; if (file_exists($path) && is_dir($path)) { // 更加严密的验证 } </code>符号链接可能导致 is_dir() 的行为不同于预期。例如,如果一个链接指向一个不存在的目录,is_dir() 也会返回 false。可以使用 is_link() 配合使用:
<code> $path = '/var/www/html/uploads'; if (is_link($path)) { echo "这是一个符号链接"; } elseif (is_dir($path)) { echo "这是一个真实目录"; } </code>权限问题:即使目录存在,如果 PHP 没有读取权限,is_dir() 也可能返回 false。
性能问题:频繁调用 is_dir() 检查大量路径可能造成 I/O 压力,建议缓存已验证路径。
多平台兼容性:路径分隔符在 Windows 和 Linux 上不同,建议使用 DIRECTORY_SEPARATOR 或统一使用 /。
$realBase = realpath($baseDir);
if ($realBase === false || !is_dir($realBase)) {
return $dirs;
}
$entries = scandir($realBase);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$fullPath = $realBase . DIRECTORY_SEPARATOR . $entry;
if (is_dir($fullPath)) {
$dirs[] = $entry;
}
}
return $dirs;
}
$uploadDirs = listUploadDirs('/var/www/gitbox.net/uploads');
print_r($uploadDirs);
</code>
这段代码安全地列出了指定目录下的所有子目录,并考虑了路径规范化与基础的安全处理。