The [is_dir](<http://php.net/is-dir>)
function returns whether the argument is a directory, while [is_file](<http://php.net/is-file>)
returns whether the argument is a file. Use [file_exists](<http://php.net/file-exists>)
to check if it is either.
$dir = "/this/is/a/directory";
$file = "/this/is/a/file.txt";
echo is_dir($dir) ? "$dir is a directory" : "$dir is not a directory", PHP_EOL,
is_file($dir) ? "$dir is a file" : "$dir is not a file", PHP_EOL,
file_exists($dir) ? "$dir exists" : "$dir doesn't exist", PHP_EOL,
is_dir($file) ? "$file is a directory" : "$file is not a directory", PHP_EOL,
is_file($file) ? "$file is a file" : "$file is not a file", PHP_EOL,
file_exists($file) ? "$file exists" : "$file doesn't exist", PHP_EOL;
This gives:
/this/is/a/directory is a directory
/this/is/a/directory is not a file
/this/is/a/directory exists
/this/is/a/file.txt is not a directory
/this/is/a/file.txt is a file
/this/is/a/file.txt exists
Use [filetype](<http://php.net/filetype>)
to check the type of a file, which may be:
fifo
char
dir
block
link
file
socket
unknown
Passing the filename to the [filetype](<http://php.net/filetype>)
directly:
echo filetype("~"); // dir
Note that filetype
returns false and triggers an E_WARNING
if the file doesn’t exist.
Passing the filename to the [is_writable](<http://php.net/is-writable>)
and [is_readable](<http://php.net/is-readable>)
functions check whether the file is writable or readable respectively.