views:

2420

answers:

6

Does anyone know a good way to check to see if a directory is writeable?
The function is_writable doesn't work for folders.

+7  A: 

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
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
A: 

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

_Lasar
+1  A: 

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).

David Dorward
A: 

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
+1  A: 

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.

Randy
+1  A: 

this is the code :)

<?php 

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

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

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

    // blah blah blah
}
Irfan EVRENS