In PHP, is_writable() tells you whether a file or folder exists and PHP is allowed to write to it. It returns true or false. is_writeable() is an alias with the same behavior.
Use it before you save data, so that your script can show a clear message when the folder or file is locked. It checks the permissions of the user that runs PHP, such as the web server user.
Syntax
Call is_writable() with the values you need. Start with this line.
PHP
$canWrite = is_writable('logs/app.log');
Call
What it does
is_writable('logs/app.log')
Returns true if that file exists and can be changed.
is_writable('logs')
Returns true if you can add new files to the logs folder.
is_writable('missing.txt')
Returns false because the file does not exist.
To check whether you can create a new file, test the folder that will hold it, not the file. A file that does not exist yet always returns false.
Library declarations
PHP
is_writable(string $filename): bool
Parameters
Parameter
Description
filename
Required. The path to the file or directory to check.
Return Value
Return Value
Description
bool
Returns true if the path exists and is writable, otherwise false.
PHP Examples
Example 1 — Check That a Folder Is Writable
The temporary folder of the system is meant for scripts to write into, so it should be writable.
PHP
<?php
$dir = sys_get_temp_dir();
if (is_writable($dir)) {
echo "You can write here", PHP_EOL;
} else {
echo "This folder is locked", PHP_EOL;
}
Output
You can write here
How It Works
sys_get_temp_dir() returns the path of the temporary folder.
is_writable() checks that the folder exists and PHP may add files to it.
The result is true, so the first message is printed.
If the folder were locked, the else branch would run instead.
Example 2 — Missing Files and Their Folders
A file that does not exist yet is not writable. To know whether you could create it, check the folder instead.