W3docs

is_writeable()

The is_writable() function is a built-in PHP function that checks whether a file is writable. This function returns true if the file is writable, and false

What the is_writable() Function Does

The is_writable() function is a built-in PHP function that checks whether a given path exists and can be written to by the current process. It works for both files and directories. It returns true when the path exists and is writable, and false otherwise — including when the path does not exist at all.

This page covers the syntax, the return value, common real-world uses, and the gotchas (caching, symlinks, root, and the deprecated is_writeable() spelling) that trip people up.

is_writeable() vs is_writable()

is_writeable() (with the extra e) is an old alias of is_writable(). It was deprecated in PHP 5.0.0 and removed in PHP 8.0.0, where calling it now raises a fatal Error. Always use the canonical spelling is_writable() in modern code.

Syntax

is_writable(string $filename): bool
ParameterDescription
$filenameThe path to the file or directory to check. May be relative (resolved against the current working directory) or absolute.

Return value: true if $filename exists and is writable, otherwise false. A non-existent path returns false rather than raising an error.

Basic Example

php— editable, runs on the server

The function returns a boolean, so it slots directly into an if condition. Here the message depends on whether the process running the script has write permission for /path/to/file.

A Safe Write Pattern

The most common use is guarding a write so your script fails gracefully instead of crashing with a permission warning:

<?php

$logFile = __DIR__ . '/app.log';

if (is_writable($logFile)) {
    file_put_contents($logFile, "Started at " . date('c') . "\n", FILE_APPEND);
    echo "Log entry written.";
} else {
    echo "Cannot write to $logFile — check permissions.";
}

Note that is_writable() checks an existing path. If the file does not exist yet, you usually want to test the directory that will contain it instead, because creating a new file requires write permission on the parent directory:

<?php

$target = __DIR__ . '/cache/data.json';

if (is_writable(dirname($target))) {
    file_put_contents($target, '{}');
    echo "File created.";
} else {
    echo "The cache directory is not writable.";
}

Things to Watch Out For

  • Results are cached. PHP caches filesystem metadata via the stat cache. If you change permissions during a script (for example with chmod()) and re-check the same path, call clearstatcache() first to get a fresh result.
  • It tests the process owner, not your login. A path that is writable in your terminal may not be writable for the web-server user (www-data, nginx, etc.) that actually runs PHP.
  • Running as root. The superuser can write almost anywhere, so is_writable() may return true even on files marked read-only. Don't rely on it as a security boundary.
  • Symlinks are followed. The check applies to the target of a symbolic link, not the link itself.
  • Race conditions (TOCTOU). A path can become unwritable between the check and the actual write. For critical writes, attempt the write and handle failure rather than relying on the check alone.
<?php

chmod('/tmp/example.txt', 0644);
var_dump(is_writable('/tmp/example.txt')); // may show stale value
clearstatcache();                          // refresh the stat cache
var_dump(is_writable('/tmp/example.txt')); // now reflects the new permissions

Conclusion

is_writable() lets you check, before writing, whether a file or directory can be written to by the current process — returning false for missing paths instead of throwing. Remember to check the parent directory when creating new files, to call clearstatcache() after changing permissions mid-script, and to use the canonical spelling since is_writeable() is gone in PHP 8.

Practice

Practice
What does the is_writable() function in PHP do?
What does the is_writable() function in PHP do?
Was this page helpful?