PHP | is_writable() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

 gfg.txt file is writable!

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.