百科狗-知识改变命运!
--

is_writable() - 判断给定的文件名是否可写 - php 文件目录函数

是丫丫呀1年前 (2023-11-21)阅读数 21#技术干货
文章标签文件

is_writable()

(PHP 4, PHP 5, PHP 7)

判断给定的文件名是否可写

说明

is_writable(string $filename): bool

如果文件存在并且可写则返回TRUE。$filename参数可以是一个允许进行是否可写检查的目录名。

记住 PHP 也许只能以运行 webserver 的用户名(通常为'nobody')来访问文件。不计入安全模式的限制。

参数

$filename

要检查的文件名称。

返回值

is_writable() - 判断给定的文件名是否可写 - php 文件目录函数

如果文件$filename存在并且可写则返回TRUE

范例

Example #1is_writable()例子

错误/异常

失败时抛出E_WARNING警告。

注释

Note:此函数的结果会被缓存。参见clearstatcache()以获得更多细节。

Tip

自 PHP 5.0.0 起,此函数也用于某些URL 包装器。请参见支持的协议和封装协议以获得支持stat()系列函数功能的包装器列表。

参见

  • is_readable() 判断给定文件名是否可读
  • file_exists() 检查文件或目录是否存在
  • fwrite() 写入文件(可安全用于二进制文件)
Be warned, that is_writable returns false for non-existent files, although they can be written to the queried path.
Check director is writable recursively. to return true, all of directory contents must be writable 
Regarding you might recognize your files on your web contructed by your PHP-scripts are grouped as NOBODY you can avoid this problem by setting up an FTP-Connection ("ftp_connect", "ftp_raw", etc.) and use methods like "ftp_fput" to create these [instead of giving out rights so you can use the usual "unsecure" way]. This will give the files created not the GROUP NOBODY - it will give out the GROUP your FTP-Connection via your FTP-Program uses, too.
Furthermore you might want to hash the password for the FTP-Connection - then check out:
http://dev.mysql.com/doc/mysql/en/Password_hashing.html
To Darek and F Dot: About group permissions, there is this note in the php.ini file:
; By default, Safe Mode does a UID compare check when
; opening files. If you want to relax this to a GID compare,
; then turn on safe_mode_gid.
safe_mode_gid = Off
This file_write() function will give $filename the write permission before writing $content to it.
Note that many servers do not allow file permissions to be changed by the PHP user. 
It appears that is_writable() does not check full permissions of a file to determine whether the current user can write to it. For example, with Apache running as user 'www', and a member of the group 'wheel', is_writable() returns false on a file like
-rwxrwxr-x      root     wheel     /etc/some.file
The results of this function seems to be not cached :
Tested on linux and windows 
In response to Darek:
We have two servers: one running PHP 5.0.4 and Apache 1.3.33, the other running PHP 4.3.5 and Apache 1.3.27. The PHP 4 server exhibits the behavior you are describing, with is_writable() returning 'false' even though the www user is in the group that owns the file, but the PHP 5 server is returning 'true.'
Check if a directory is writable. Work also on mounted SMB shares:
function isWritablePath($home, $xpath) {
  $isOK = false;
  $path = trim($xpath);
  if ( ($path!="") && (strpos($path,$home)!==false) && is_dir($path) && is_writable($path) ) {
    $tmpfile = "mPC_".uniqid(mt_rand()).'.writable';
    $fullpathname = str_replace('//','/',$path."/".$tmpfile);
    $fp = @fopen($fullpathname,"w");
    if ($fp !== false) {
      $isOK = true;
    }
    @fclose($fp);
    @unlink($fullpathname);
  }
  return $isOK;
  
}
This is the latest version of is__writable() I could come up with.
It can accept files or folders, but folders should end with a trailing slash! The function attempts to actually write a file, so it will correctly return true when a file/folder can be written to when the user has ACL write access to it. 
Since looks like the Windows ACLs bug "wont fix" (see http://bugs.php.net/bug.php?id=27609) I propose this alternative function:

It should work both on *nix and Windows
NOTE: you must use a trailing slash to identify a directory
function is_writable('ftp://user.....') always return false. I can create/delete files, but can check is writable. Is this bug or php feature :)?
I'd like to also clarify a point on this. Even if you see 777 permissions for the directly, you may need to check your ACL, since your server's group might not have write permissions there.
Under Windows, it only returns the read-only attribute status, not the actual permissions (ACL).
See http://bugs.php.net/bug.php?id=27609

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)