How do I check if a directory is writeable in PHP?

2019-02-05 10:30发布

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

9条回答
别忘想泡老子
2楼-- · 2019-02-05 10:55

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

查看更多
我命由我不由天
3楼-- · 2019-02-05 11:09

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>';
    } 
}
?>
查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-05 11:10

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
}
查看更多
登录 后发表回答