Open In App

PHP | is_writable() Function

Last Updated : 05 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The is_writable() function in PHP used to check whether the specified file is writable or not. The name of the file is sent as a parameter to the is_writable() function and it returns True if the filename exists and is writable.
Name of a directory can also be a parameter to the is_writable() function which allows checking whether the directory is writable or not.

Syntax:

is_writable(file)

Parameters Used:
The is_writable() function in PHP accepts one parameter.

  • file : It is a mandatory parameter which specifies the file.

Return Value:
It returns True if the filename exists and is writable.

Exceptions:

  1. An E_WARNING is emitted on failure.
  2. The result of this function are cached and therefore the clearstatcache() function is used to clear the cache.
  3. is_writable() function returns false for non-existent files.

Examples:

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

Below programs illustrate the is_writable() function.

Program 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!");
}
?>


Output:

 gfg.txt file is writable!

Program 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();
?>


Output:

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

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads