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

前端 未结 10 1409
别跟我提以往
别跟我提以往 2021-01-01 08:07

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 doe

相关标签:
10条回答
  • 2021-01-01 09:01

    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
    }
    
    0 讨论(0)
  • 2021-01-01 09:01

    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>';
        } 
    }
    ?>
    
    0 讨论(0)
  • 2021-01-01 09:05

    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.

    0 讨论(0)
  • 2021-01-01 09:05

    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

    0 讨论(0)
提交回复
热议问题