PHP is_writeable() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, is_writeable() checks whether a file or folder exists and can be written to. It is an alias of is_writable(), so both names do exactly the same thing.

Use it before saving data, so your script can show a friendly message instead of a warning when the file is locked or has the wrong permissions.

Syntax

Call is_writeable() with the values you need. Start with this line.

PHP
$canWrite = is_writeable('logs/app.log');
CallWhat it does
is_writeable('notes.txt')Returns true if notes.txt exists and your script is allowed to change it.
is_writeable(__DIR__)Checks whether the current folder accepts new files.
is_writeable('missing.txt')Returns false because the file does not exist.

is_writeable() is only an alias. Most tutorials and the official manual use the spelling is_writable(), so pick one name and use it everywhere in your project. The result is cached by PHP for the same path, so call clearstatcache() if you change permissions during the same request.

Library declarations
PHP
is_writeable(string $filename): bool

Parameters

ParameterDescription
filenameRequired. The path to the file or directory you want to test.

Return Value

Return ValueDescription
boolReturns true if the path exists and is writable, otherwise false.

PHP Examples

Example 1 — Check a File Before Editing

Create a small file and test whether PHP is allowed to change it.

PHP
<?php
$file = __DIR__ . '/notes.txt';
file_put_contents($file, 'Hello');

if (is_writeable($file)) {
    echo "notes.txt can be edited", PHP_EOL;
} else {
    echo "notes.txt is read only", PHP_EOL;
}

How It Works

  1. file_put_contents() creates notes. txt so the file really exists.
  2. is_writeable() asks the system if the current user may write to it.
  3. The file was just created by this script, so the answer is true.
  4. The first message is printed.

Example 2 — Missing Files Are Not Writable

is_writeable() returns false when the path does not exist, even if the folder itself is writable.

PHP
<?php
$file = __DIR__ . '/missing/log.txt';

var_dump(is_writeable($file));
var_dump(is_writeable(__DIR__));

How It Works

  1. The missing folder means the file path points to nothing.
  2. is_writeable() cannot say yes to something that does not exist, so it returns false.
  3. The current folder exists and is writable, so the second call returns true.
  4. If you want to create a new file, test the folder instead of the file.

Example 3 — Write to a Log Only When Allowed

Wrap the check in a small function so every log message is saved safely.

PHP
<?php
$log = __DIR__ . '/app.log';
touch($log);

function addLog($file, $message) {
    if (!is_writeable($file)) {
        return "Cannot write to " . basename($file);
    }

    file_put_contents($file, $message . PHP_EOL, FILE_APPEND);
    return "Saved: $message";
}

echo addLog($log, 'Server started'), PHP_EOL;
echo addLog(__DIR__ . '/nope/app.log', 'Hello'), PHP_EOL;

How It Works

  1. touch() makes sure app. log exists and can be tested.
  2. addLog() first calls is_writeable() and stops early with a message if the answer is false.
  3. The first call writes the line using FILE_APPEND, so old lines are kept.
  4. The second call points to a folder that does not exist, so the function returns the error text.

Frequently Asked Questions

It checks whether a file or folder exists and can be written by the user running PHP. It returns true or false.
Yes. is_writeable() is an alias of is_writable(). The second spelling is more common in documentation, so many developers prefer it.
The file must already exist. To check whether you can create a new file, call is_writeable() on the folder that will contain it.
Yes. A folder is writable when PHP can add, rename or delete files inside it.
PHP caches file status information. Call clearstatcache() after changing permissions with chmod() if you test the same path again.

Next: lchgrp()

lchgrp() changes the group of a symbolic link itself. It does not follow the link to the f.

lchgrp() 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