如何检查目录是否存在? "is_dir","file_exists"或两者? [英] How do I check if a directory exists? "is_dir", "file_exists" or both?

查看:154
本文介绍了如何检查目录是否存在? "is_dir","file_exists"或两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果目录不存在,我想创建一个目录.

I want to create a directory if it does'nt exist already.

为此目的使用is_dir是否足够?

Is using is_dir enough for that purpose?

if ( !is_dir( $dir ) ) {
    mkdir( $dir );       
}

还是应该将is_dirfile_exists结合使用?

Or should I combine is_dir with file_exists?

if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
    mkdir( $dir );       
} 

推荐答案

在Unix系统上,两者都将返回true-在Unix中,所有内容都是文件,包括目录.但是要测试该名称是否被采用,您应该同时检查两者.可能会有一个名为"foo"的常规文件,这将阻止您创建目录名称"foo".

Both would return true on Unix systems - in Unix everything is a file, including directories. But to test if that name is taken, you should check both. There might be a regular file named 'foo', which would prevent you from creating a directory name 'foo'.

这篇关于如何检查目录是否存在? "is_dir","file_exists"或两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆