I want to create a directory if it does not exist already.
Is using the is_dir
function enough for that purpose?
if [ !is_dir[ $dir ] ] {
mkdir[ $dir ];
}
Or should I combine is_dir
with file_exists
?
if [ !file_exists[ $dir ] && !is_dir[ $dir ] ] {
mkdir[ $dir ];
}
asked Mar 24, 2011 at 21:38
6
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'.
answered Mar 24, 2011 at 21:42
Marc BMarc B
351k42 gold badges402 silver badges486 bronze badges
3
$dirname = $_POST["search"];
$filename = "/folder/" . $dirname . "/";
if [!file_exists[$filename]] {
mkdir["folder/" . $dirname, 0777];
echo "The directory $dirname was successfully created.";
exit;
} else {
echo "The directory $dirname exists.";
}
nickb
58.5k12 gold badges102 silver badges141 bronze badges
answered Jun 8, 2011 at 10:50
MaherMaher
1,5091 gold badge9 silver badges2 bronze badges
7
I think realpath[] may be the best way to validate if a path exist //www.php.net/realpath
Here is an example function: