PHP is_writable() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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');
CallWhat 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

ParameterDescription
filenameRequired. The path to the file or directory to check.

Return Value

Return ValueDescription
boolReturns 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;
}

How It Works

  1. sys_get_temp_dir() returns the path of the temporary folder.
  2. is_writable() checks that the folder exists and PHP may add files to it.
  3. The result is true, so the first message is printed.
  4. 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.

PHP
<?php
$file = sys_get_temp_dir() . '/is_writable_new.txt';

echo "Missing file writable: ", is_writable($file) ? 'yes' : 'no', PHP_EOL;
echo "Its folder writable: ", is_writable(dirname($file)) ? 'yes' : 'no', PHP_EOL;

How It Works

  1. The file path points to a file that was never created, so is_writable() returns false.
  2. dirname() returns the folder part of the path.
  3. That folder exists and allows writing, so the second check returns true.
  4. This means the script could safely create the new file.

Example 3 — Guard a Log Write

Check that the log file can be changed before you append a new line to it.

PHP
<?php
$log = sys_get_temp_dir() . '/is_writable_app.log';
file_put_contents($log, 'Started' . PHP_EOL);

if (is_writable($log)) {
    file_put_contents($log, 'Saved' . PHP_EOL, FILE_APPEND);
    echo "Log line saved", PHP_EOL;
} else {
    echo "Log file is not writable", PHP_EOL;
}

echo count(file($log)), " lines in the log", PHP_EOL;
unlink($log);

How It Works

  1. The log file is created with one starting line.
  2. is_writable() returns true, so the script appends a second line using FILE_APPEND.
  3. FILE_APPEND keeps the old content instead of replacing it.
  4. file() reads the log into an array of lines, and count() reports 2.

Frequently Asked Questions

It checks whether a file or folder exists and can be written to by the user that runs PHP.
No. is_writeable() is an alias of is_writable() and works the same way. Use is_writable() in new code.
Check the folder with is_writable(dirname($path)). A file that does not exist yet always returns false.
PHP may run as a user who has no write permission, or the file may be read only. Check the permissions and the owner of the file.
No. The disk can be full or the file can be locked after the check. Always check the result of fwrite() or file_put_contents() as well.

Next: is_writeable()

is_writeable() checks whether a file or folder exists and can be written to. It is an alia.

is_writeable() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful