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

时间:2023-02-08 07:12:00

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

有人知道我如何检查一个目录是否在PHP中是可写的吗?

The function is_writable doesn't work for folders. (edit: It does work. See the accepted answer.)

is_writable函数不适用于文件夹。(编辑:它的工作。见接受答案。)

9 个解决方案

#1


84  

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。文件名参数可能是一个目录名,允许您检查一个目录是否可写。

#2


14  

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
}

#3


5  

to be more specific for owner/group/world

对于所有者/组/世界更具体

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

peace...

和平……

#4


4  

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_writable will correctly tell you whether the directory is writable or not. If $file contains 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.

我希望这能帮助某人。

#5


3  

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

(之前的一篇仓促的阅读让我觉得拖尾斜杠很重要,但事实证明,这是针对这方面的工作的)。

#6


1  

stat()

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中。您要检查的是模式值,就像您在其他语言(例如C/ c++)调用stat时所做的那样。

http://us2.php.net/stat

http://us2.php.net/stat

#7


0  

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

根据PHP手册,is_writable应该在目录上运行良好。

#8


0  

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

#9


0  

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

#1


84  

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。文件名参数可能是一个目录名,允许您检查一个目录是否可写。

#2


14  

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
}

#3


5  

to be more specific for owner/group/world

对于所有者/组/世界更具体

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

peace...

和平……

#4


4  

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_writable will correctly tell you whether the directory is writable or not. If $file contains 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.

我希望这能帮助某人。

#5


3  

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

(之前的一篇仓促的阅读让我觉得拖尾斜杠很重要,但事实证明,这是针对这方面的工作的)。

#6


1  

stat()

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中。您要检查的是模式值,就像您在其他语言(例如C/ c++)调用stat时所做的那样。

http://us2.php.net/stat

http://us2.php.net/stat

#7


0  

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

根据PHP手册,is_writable应该在目录上运行良好。

#8


0  

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

#9


0  

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