当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP is_writable()用法及代码示例


PHP中的is_writable()函数用于检查指定的文件是否可写。文件名作为参数发送到is_writable()函数,如果文件名存在且可写,则返回True。
目录的名称也可以是is_writable()函数的参数,该函数允许检查目录是否可写。

用法:

is_writable(file)

使用的参数:
PHP中的is_writable()函数接受一个参数。


  • file :它是指定文件的必需参数。

返回值:
如果文件名存在且可写,则返回True。

异常:

  1. 失败时发出E_WARNING。
  2. 此函数的结果被缓存,因此使用clearstatcache()函数清除缓存。
  3. is_writable()函数为不存在的文件返回false。

例子:

Input : $myfile = "gfg.txt";
        if(is_writable($myfile)) 
        {
           echo ("$myfile file is writable!");
        } 
        else 
        {
           echo ("$myfile file is not writable!");
        }
Output : gfg.txt file is writable!

Input : $permissions = fileperms("gfg.txt");
        $perm_value = sprintf("%o", $permissions);
        $myfile = "gfg.txt";
       
        if (is_writable($myfile)) 
        {
          echo ("$myfile file is writable and
          it has the following file permissions : $perm_value");
        } 
        else 
        {
          echo ("$myfile file is not writable and
          it has the following file permissions : $perm_value");
        }

Output : gfg.txt file is writable and it has the following file permissions : 0664

以下示例程序旨在说明is_writable()函数。

程序1

<?php  
$myfile = "gfg.txt"; 
  
// checking whether the file is writable or not 
if(is_writable($myfile))  
{ 
 echo ("$myfile file is writable!"); 
}  
else 
{ 
 echo ("$myfile file is not writable!"); 
} 
?>

输出:

 gfg.txt file is writable!

程序2

<?php  
// checking permissions of the file 
$permissions = fileperms("gfg.txt"); 
$perm_value = sprintf("%o", $permissions); 
  
// Clearing the File Status Cache  
clearstatcache(); 
  
$myfile = "gfg.txt"; 
  
// checking whether the file is writable or not 
if(is_writable($myfile))  
{ 
 echo ("$myfile file is writable and 
   it has the following file permissions : $perm_value"); 
}  
else 
{ 
 echo ("$myfile file is not writable and
   it has the following file permissions : $perm_value"); 
} 
  
// Clearing the File Status Cache  
clearstatcache(); 
?>

输出:

gfg.txt file is writable and it has the following file permissions : 0664

参考:
http://php.net/manual/en/function.is-writable.php



相关用法


注:本文由纯净天空筛选整理自Shubrodeep Banerjee大神的英文原创作品 PHP | is_writable() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。