How to Use PHP’s is_writable() to Check File and Directory Permissions

This guide explains the PHP is_writable() function, its syntax, and provides clear examples for checking whether files or directories are writable, how to combine it with file existence checks, and how to determine write permissions in various scenarios.

php Courses
php Courses
php Courses
How to Use PHP’s is_writable() to Check File and Directory Permissions

Syntax of is_writable()

bool is_writable ( string $filename )

The $filename parameter specifies the path of the file or directory to be examined.

Using is_writable()

Check if a file is writable

The function returns true when the file exists and is writable; otherwise it returns false. Example:

$filename = 'example.txt';
if (is_writable($filename)) {
    echo 'File is writable';
} else {
    echo 'File is not writable';
}

This code defines $filename with the target file path, calls is_writable(), and outputs the result based on the return value.

Check if a directory is writable

The same function can test directory write permissions. Example:

$directory = 'example';
if (is_writable($directory)) {
    echo 'Directory is writable';
} else {
    echo 'Directory is not writable';
}

Here $directory holds the directory path, and is_writable() determines its writeability.

Check file existence before testing writability

Combine file_exists() with is_writable() to handle missing files:

$filename = 'example.txt';
if (file_exists($filename)) {
    if (is_writable($filename)) {
        echo 'File exists and is writable';
    } else {
        echo 'File exists but is not writable';
    }
} else {
    echo 'File does not exist';
}

This script first verifies the file's existence, then checks its write permission.

Check write permission of a file or directory

Use is_writable() directly to test whether a path has write permission, regardless of it being a file or a directory:

$filename = 'example.txt';
if (is_writable($filename)) {
    echo 'File or directory has write permission';
} else {
    echo 'File or directory does not have write permission';
}

The function returns true if the specified path is writable, otherwise false.

Summary

The is_writable() function is a simple yet powerful tool for determining whether a file or directory can be written to; by passing the target path, developers can quickly assess write permissions and handle files accordingly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentphp-functionsis_writablefile-permission
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.