How to check if a directory can be written in PHP?

Does anyone know how I can check if a directory can be written in PHP?

The is_writable function does not work for folders. (edit: it works. See accepted answer.)

+49
directory php permissions
Sep 20 '08 at 20:07
source share
9 answers

Yes, it works for folders ....

Returns TRUE if the file name exists and is writable. The filename argument can be a directory name to check if the directory is writable.

+79
Sep 20 '08 at 20:09
source share

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 } 
+12
Sep 23 2018-10-23T00:
source share

to be more specific to the owner / group / world

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

peace...

+5
May 25 '11 at 7:38
source share

According to the documentation for is_writable , it should just work, but you said β€œfolder”, so it could be a Windows problem . The comments suggest a workaround .

(A quick read earlier made me think trailing slashes are important, but that turned out to be specific to this work).

+3
Sep 20 '08 at 20:10
source share

You can send the full path to the is_writable() function file. is_writable() will return false if the file does not already exist in the directory. You need to check the directory itself with the removal of the file name, if so. If you do, is_writable will correctly indicate whether the directory is writable or not. If $file contains your file path, follow these steps:

 $file_directory = dirname($file); 

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

I hope this helps someone.

+3
Oct 13 '09 at 16:31
source share

stat ()

Like a system stat, but in PHP. What you want to check is the mode value, like any other stat call in other languages ​​(IEC / C ++).

http://us2.php.net/stat

+1
Sep 20 '08 at 20:12
source share

According to the PHP manual, is_writable should work fine in directories.

0
Sep 20 '08 at 20:09
source share

this is how i do it:

create a file with file_put_contents() and check the return value, if it is positive (the number of bytes written), then you can go and do what you need 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 using the unlink () function

 unlink('directory/dummy.txt'); 
0
Jun 16 '15 at 12:11
source share

I wrote 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>'; } } ?> 
0
Aug 10 '17 at 2:39 on
source share



All Articles