PHP判断文件或者目录是否可写
2012-12-22 21:13
197 查看
在PHP中,可用is_writable()函数来判断一个 文件/目录 是否可写,详情如下:
参考
is_writable
(PHP 4, PHP 5)
is_writable — 判断给定的文件名是否可写
说明
bool is_writable ( string $filename )
如果文件存在并且可写则返回 TRUE。($filename 参数可以是一个目录名,即检查目录是否可写。 )
记住 PHP 也许只能以运行 webserver 的用户名(通常为 ‘nobody’)来访问文件。不计入安全模式的限制。
Example #1 is_writable() 例子
注意:is_writeable() 是 is_writable() 的别名!
+++++++++++++++++++++++++++++++++++++++++++++++++
但是,上面那个函数在PHP4中是有BUG的,尤其是在Windows服务器下判断不准,官方相关bug报告链接如下:
http://bugs.php.net/bug.php?id=27609
为了兼容各个操作系统,可自定义一个判断可写函数,代码如下:
参考
is_writable
(PHP 4, PHP 5)
is_writable — 判断给定的文件名是否可写
说明
bool is_writable ( string $filename )
如果文件存在并且可写则返回 TRUE。($filename 参数可以是一个目录名,即检查目录是否可写。 )
记住 PHP 也许只能以运行 webserver 的用户名(通常为 ‘nobody’)来访问文件。不计入安全模式的限制。
<?php 2 $filename = 'test.txt'; 3 if (is_writable($filename)) { 4 echo 'The file is writable'; 5 } else { 6 echo 'The file is not writable'; 7 } 8 ?>
Example #1 is_writable() 例子
注意:is_writeable() 是 is_writable() 的别名!
+++++++++++++++++++++++++++++++++++++++++++++++++
但是,上面那个函数在PHP4中是有BUG的,尤其是在Windows服务器下判断不准,官方相关bug报告链接如下:
http://bugs.php.net/bug.php?id=27609
为了兼容各个操作系统,可自定义一个判断可写函数,代码如下:
/** 02 * 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数) 03 * 04 * @param string $file 文件/目录 05 * @return boolean 06 */ 07 function new_is_writeable($file){ 08 if (is_dir($file)){ 09 $dir = $file; 10 if ($fp = @fopen("$dir/test.txt", 'w')) { 11 @fclose($fp); 12 @unlink("$dir/test.txt"); 13 $writeable = 1; 14 } else { 15 $writeable = 0; 16 } 17 } else { 18 if ($fp = @fopen($file, 'a+')) { 19 @fclose($fp); 20 $writeable = 1; 21 } else { 22 $writeable = 0; 23 } 24 } 25 26 return $writeable; 27 }
相关文章推荐
- PHP判断文件或者目录是否可写
- PHP判断文件或者目录是否可写
- shell判断文件,目录是否存在或者具有权限的代码
- php 判断文件/目录是否存的方法
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限的代码
- shell判断文件,目录是否存在或者具有权限
- node.js 判断文件或者目录是否存在
- Shell脚本中判断文件,目录是否存在或者具有权限 (原创)
- shell判断文件,目录是否存在或者具有权限
- ios6.1&ios7&ios9判断沙盒文件或者目录是否存在,以及判断是文件还是目录的一个隐藏问题
- shell判断文件,目录是否存在或者具有权限 (转载)
- PHP如何判断文件、目录是否存在?
- PHP 判断远程或者本地文件是否存在
- shell判断文件,目录是否存在或者具有权限的代码
- 【shell】判断一个文件或者目录是否存在
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- [IO]判断文件或者目录是否存在