Check string for valid directory name

I need a function, native or custom, that checks if a string is a valid directory name. I looked through the PHP documentation and I can only find a function is_dirthat checks if this existing directory exists or not, and not a valid name for the directory.

+5
source share
2 answers

This should work for you.

See regex for checking folder name and file name

if (strpbrk($filename, "\\/?%*:|\"<>") === FALSE) {
  /* $filename is legal; doesn't contain illegal character. */
}
else {
  /* $filename contains at least one illegal character. */
}
+7
source

It will work. You can change the second and third arguments to suit your needs.

if (!mkdir($directory, 0700, true)) {
    die('Error: Illegal directory.');
}

, , . , (, ), . , , , .

, , , .

, , .

, , mkdir() (, , , ..), . , PHP , , , , , , mkdir().

+4

All Articles