PHP is_writable() Function

What is PHP is_writable() Function?

If you want to know whether a file or directory exist or not and is writable, use PHP is_writable() function. In your web app development, you want to check whether a file or folder is writable before you write on it. In this case you can use this function.

Syntax:

is_writable(filename)

Parameters:

The Function has 1 parameter which is required-

filename (Required): It is the path to the file or directory whose writability you want to check.

Return Values:

The function returns-

  • TRUE – if the parameter is a file or directory and is writable.
  • FALSE – if the parameter doesn’t exist; hence not writable or the file exists but not writable.

How to use is_writable() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result that the conditional statement returns, your program flow progresses. Look at the code below-

<?php
if (is_readable("file/demo.txt")) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}
?>

Examples:

Example 1: Confirming that the file/folder exists and not writable-

<?php
$filename = "file/denied.txt";
if(file_exists($filename)){
    if (is_writable($filename)) {
        echo 'The file is writable.';
    } else {
        echo 'The file exists but is not writable.';
    }
}else{
    echo "File doesn't exist.";
}
?>

Output:

The file exists but is not writable.

Explanation:

Here, file_exists() function confirms that that file “denied.txt” exists. And the is_writable() function confirms that the file exists and is not writable.

Example 2: Checking whether an entry is a file and is writable-

<?php
$filename = "file/demo.txt";
if (is_file($filename) && is_writable($filename)) {
    echo "It is a file and it is writable.";
}else{
    echo "It is not a file.";
}
?>

Output:

It is a file and it is writable.

Explanation:

The is_file() function confirms that “demo.txt” is a file and the is_writable() function confirms that the file is writable. Otherwise the output would be “It is not a file.”

Example 3: Checking whether an entry is a directory and is writable-

<?php
$filename = "file/";
if (is_dir($filename) && is_writable($filename)) {
    echo "It is a directory.";
}else{
    echo "It is not a directory.";
}
?>

Output:

It is a directory and it is writable.

Explanation:

The is_dir() function confirms that “file/” is a directory and the is_writable() function confirms that the directory is writable. Otherwise, the output would be “It is not a directory.”

Notes on is_writable() Function:

  • To check whether the file exists or not, you don’t need to use file_exists() with is_writable() function. Because, when is_writable() function returns TRUE, it confirms the file’s existence.
  • But, when the function returns FALSE, it may happen that the file doesn’t exists or the file exists but not writable. So, confirming file’s existence with file_exists() function, if you use is_writable() function, and it returns FALSE, it means that the file is not writable. Check example 1.
  • As the function returns TRUE for both file and directory, you can use is_file() function with this function to ensure that it is a file. Check example 2.
  • Similarly, as the function returns TRUE for both file and directory, you can use is_dir() function with this function to ensure that it is a directory. Check example 3.

Caution:

  • PHP cache the result of the is_writable() function. So if you call this function multiple times in a script PHP sends you cached result. The problem is if you modify the file, you won’t get the up-to-date result of the file. So, to get the latest result, clear the cache with clearstatcache() function. Check the code below-
    <?php
    $filename = "file/denied.txt";
    var_dump(is_writable($filename));
    // Some codes.
    var_dump(is_writable($filename));
    // Some codes.
    clearstatcache();
    var_dump(is_writable($filename));
    // Some codes.
    ?>
    

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_writable() Function

is_writable() which is one of the built-in PHP filesystem functions is very useful when you work with files and directories. It confirms you whether an entry is file/directory and then you can do the other related tasks.

Reference:

https://www.php.net/manual/en/function.is-writable.php