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

前端 未结 10 1628
别跟我提以往
别跟我提以往 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 08:45

    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');
    

提交回复
热议问题