Thursday, October 9, 2025
HomeLanguagesPHP | is_writable() Function

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




<?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

RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11876 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS