PHP is_writable() Function
The PHP is_writable() function checks whether a file exists and is writable.
Note: The results of this function are cached. Use clearstatcache() function to clear the cache.
Syntax
is_writable(filename)
Parameters
filename |
Required. Specify the path to the file to check. |
Return Value
Returns true if the file or directory specified by filename exists and is writable, otherwise returns false.
Note: This function may return true for directories. Use is_dir() to distinguish file and directory.
Exceptions
Upon failure, an E_WARNING is thrown.
Example:
Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to check whether a file exists and is writable.
<?php $file1 = 'test.txt'; $file2 = 'demo.txt'; //checking if $file1 is writable if(is_writable($file1)) { echo "The file $file1 is writable.\n"; } else { echo "The file $file1 is not writable.\n"; } //checking if $file2 is writable if(is_writable($file2)) { echo "The file $file2 is writable.\n"; } else { echo "The file $file2 is not writable.\n"; } ?>
The output of the above code will be:
The file test.txt is writable. The file demo.txt is not writable.
❮ PHP Filesystem Reference