如何检查目录是否可在 PHP 中写入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/109188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:29:05  来源:igfitidea点击:

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

phppermissionsdirectory

提问by SeanDowney

Does anyone know how I can check to see if a directory is writeable in PHP?

有谁知道我如何检查目录是否可在 PHP 中写入?

The function is_writabledoesn't work for folders.

该功能is_writable不适用于文件夹。

Edit: It does work. See the accepted answer.

编辑:它确实有效。请参阅已接受的答案。

回答by DGM

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.

如果文件名存在且可写,则返回 TRUE。filename 参数可能是一个目录名,允许您检查目录是否可写。

回答by Irfan EVRENS

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
}

回答by Griffith

to be more specific for owner/group/world

更具体地针对所有者/组/世界

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

peace...

和平...

回答by Griffith

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_writablewill correctly tell you whether the directory is writable or not. If $filecontains your file path do this:

您可能正在向is_writable()函数发送完整的文件路径。is_writable()如果文件不存在于目录中,将返回 false。如果是这种情况,您需要检查删除了文件名的目录本身。如果你这样做,is_writable会正确地告诉你目录是否可写。如果$file包含您的文件路径,请执行以下操作:

$file_directory = dirname($file);

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

然后用于is_writable($file_directory)确定文件夹是否可写。

I hope this helps someone.

我希望这可以帮助别人。

回答by Quentin

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.

根据is_writable 的文档,它应该可以正常工作-但是您说的是“文件夹”,因此这可能是Windows 问题。评论提出了一种解决方法

(A rushed reading earlier made me think that trailing slashes were important, but that turned out to be specific to this work around).

(早些时候匆忙阅读让我认为尾部斜杠很重要,但结果证明这是特定于这项工作的)。

回答by Studocwho

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.

我编写了一个小脚本(我称之为isWritable.php),它检测脚本所在目录中的所有目录,并将每个目录是否可写写入页面。希望这可以帮助。

<?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>';
    } 
}
?>

回答by Jason Mock

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

很像系统统计信息,但在 PHP 中。您要检查的是模式值,就像您在其他语言(IE C/C++)中对 stat 的任何其他调用一样。

http://us2.php.net/stat

http://us2.php.net/stat

回答by Lasar

According to the PHP manual is_writableshould work fine on directories.

根据 PHP 手册is_writable应该可以在目录上正常工作。

回答by Nassim

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

创建一个文件file_put_contents()并检查返回值,如果它是正数(以字节为单位写入的数量)那么你可以继续做你必须做的事情,如果它是 FALSE 那么它是不可写的

$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() 删除虚拟文件

unlink('directory/dummy.txt');

回答by Andron

In my case, is_writablereturned true, but when tried to write the file - an error was generated.
This code helps to check if the $direxists and is writable:

在我的情况下,is_writable返回 true,但是当尝试写入文件时 - 生成了错误。
此代码有助于检查是否$dir存在且可写:

<?php
$dir = '/path/to/the/dir';

// try to create this directory if it doesn't exist
$booExists     = is_dir($dir) || (mkdir($dir, 0774, true) && is_dir($dir));
$booIsWritable = false;
if ($booExists && is_writable($dir)) {
    $tempFile = tempnam($dir, 'tmp');
    if ($tempFile !== false) {
        $res = file_put_contents($tempFile, 'test');

        $booIsWritable = $res !== false;
        @unlink($tempFile);
    }
}