PHP provides several built-in filesystem is_* functions that help determine the type, status, permissions, and existence of files and directories. These checks are essential before performing file operations.
// Demonstrating PHP filesystem is_* functions
<?php
$file = "example.txt";
$directory = "myDirectory";
if (is_file($file)) {
echo "$file is a valid file.<br>";
} else {
echo "$file is not a valid file.<br>";
}
if (is_dir($directory)) {
echo "$directory is a valid directory.<br>";
} else {
echo "$directory is not a valid directory.<br>";
}
if (is_readable($file)) {
echo "$file is readable.<br>";
} else {
echo "$file is not readable.<br>";
}
if (is_writable($directory)) {
echo "$directory is writable.<br>";
} else {
echo "$directory is not writable.<br>";
}
if (is_executable($file)) {
echo "$file is executable.<br>";
} else {
echo "$file is not executable.<br>";
}
if (is_link($file)) {
echo "$file is a symbolic link.<br>";
} else {
echo "$file is not a symbolic link.<br>";
}
?>
The script evaluates file properties and prints results based on the actual permissions and type of each path on the server.
Think of these functions as security gates. Each function confirms whether PHP is allowed to interact with a file safely before executing operations like reading, writing, or running it.
scandir()