當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。