63

Does anyone know how I can check to see if a directory is writeable in PHP?

The function is_writable doesn't work for folders.

Edit: It does work. See the accepted answer.

reformed
  • 4,505
  • 11
  • 62
  • 88
SeanDowney
  • 17,368
  • 20
  • 81
  • 90

10 Answers10

101

Yes, it does work for folders....

Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.

DGM
  • 26,629
  • 7
  • 58
  • 79
  • 28
    The trick is that you can't specify a *file* that doesn't exist yet within the folder you really want to test - just specify the folder. – philfreo Sep 24 '10 at 17:33
25

this is the code :)

<?php 

$newFileName = '/var/www/your/file.txt';

if ( ! is_writable(dirname($newFileName))) {

    echo dirname($newFileName) . ' must writable!!!';
} else {

    // blah blah blah
}
Irfan EVRENS
  • 439
  • 4
  • 9
  • Ahh, `dirname($new_file_name)`, that's simpler than what I was going to do. I was going to use `pathinfo($new_file_name,PATHINFO_DIRNAME)`. Thanks. – Buttle Butkus Apr 24 '14 at 07:57
7

to be more specific for owner/group/world

$dir_writable = substr(sprintf('%o', fileperms($folder)), -4) == "0774" ? "true" : "false";

peace...

Griffith
  • 79
  • 1
  • 1
  • 2
    This function should be improved. Right now it would return false for permissions like `0777`. – Slava Jan 27 '16 at 13:27
5

You may be sending a complete file path to the is_writable() function. is_writable() will return false if the file doesn't already exist in the directory. You need to check the directory itself with the filename removed, if this is the case. If you do that, is_writable will correctly tell you whether the directory is writable or not. If $file contains your file path do this:

$file_directory = dirname($file);

Then use is_writable($file_directory) to determine if the folder is writable.

I hope this helps someone.

A J
  • 3,970
  • 14
  • 38
  • 53
4

According to the documentation for is_writable, it should just work - but you said "folder", so this could be a Windows issue. The comments suggest a workaround.

(A rushed reading earlier made me think that trailing slashes were important, but that turned out to be specific to this work around).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

I've written a little script (I call it isWritable.php) that detects all directories in the same directory the script is in and writes to the page whether each directory is writable or not. Hope this helps.

<?php
// isWritable.php detects all directories in the same directory the script is in
// and writes to the page whether each directory is writable or not.

$dirs = array_filter(glob('*'), 'is_dir');

foreach ($dirs as $dir) {
    if (is_writable($dir)) {
        echo $dir.' is writable.<br>';
    } else {
        echo $dir.' is not writable. Permissions may have to be adjusted.<br>';
    } 
}
?>
Studocwho
  • 2,404
  • 3
  • 23
  • 29
2

stat()

Much like a system stat, but in PHP. What you want to check is the mode value, much like you would out of any other call to stat in other languages (I.E. C/C++).

http://us2.php.net/stat

Jason Mock
  • 823
  • 1
  • 10
  • 19
1

According to the PHP manual is_writable should work fine on directories.

Lasar
  • 5,175
  • 4
  • 24
  • 22
1

In my case, is_writable returned true, but when tried to write the file - an error was generated.
This code helps to check if the $dir exists and is writable:

<?php
$dir = '/path/to/the/dir';

// try to create this directory if it doesn't exist
$booExists     = is_dir($dir) || (mkdir($dir, 0774, true) && is_dir($dir));
$booIsWritable = false;
if ($booExists && is_writable($dir)) {
    $tempFile = tempnam($dir, 'tmp');
    if ($tempFile !== false) {
        $res = file_put_contents($tempFile, 'test');

        $booIsWritable = $res !== false;
        @unlink($tempFile);
    }
}
Andron
  • 6,413
  • 4
  • 43
  • 56
0

this is how I do it:

create a file with file_put_contents() and check the return value, if it is positive (number of written in Bytes) then you can go ahead and do what you have to do, if it is FALSE then it is not writable

$is_writable = file_put_contents('directory/dummy.txt', "hello");

if ($is_writable > 0) echo "yes directory it is writable";

else echo  "NO directory it is not writable";

then you can delete the dummy file by using unlink()

unlink('directory/dummy.txt');
Nassim
  • 2,879
  • 2
  • 37
  • 39
  • While this technically works, it is definitely not the recommended method. Filesystem operations are relatively slow and incur a lot of overhead, so creating a dummy file and then deleting/unlinking it is **much** slower than most other methods. – Byson Jul 29 '15 at 11:27