← Back to Chapters

PHP FileSystem is_* Functions

? PHP FileSystem is_* Functions

? Quick Overview

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.

? Key Concepts

  • File vs directory detection
  • Permission checking
  • Executable and symbolic link validation
  • Safe filesystem handling

? Syntax & Theory

  • is_file(path) – checks regular file
  • is_dir(path) – checks directory
  • is_readable(path) – checks read permission
  • is_writable(path) – checks write permission
  • is_executable(path) – checks execute permission
  • is_link(path) – checks symbolic link
  • file_exists(path) – checks existence

? Code Example

? View Code Example
// 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>";
}
?>

? Live Output / Explanation

The script evaluates file properties and prints results based on the actual permissions and type of each path on the server.

? Interactive Concept

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.

?️ Use Cases

  • Validating uploads
  • Checking folder permissions before writing logs
  • Ensuring scripts run only executable files
  • Preventing errors in file handling

✅ Tips & Best Practices

  • Always validate file existence before access
  • Check permissions explicitly for safer code
  • Handle symbolic links carefully

? Try It Yourself

  • Test with real file paths on your system
  • Check directory emptiness using scandir()
  • Build permission-aware file upload logic
  • Detect executable scripts dynamically