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:
- An E_WARNING is emitted on failure.
- The result of this function are cached and therefore the clearstatcache() function is used to clear the cache.
- 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
Recommended Posts:
- PHP | SplFileInfo isWritable() Function
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Set contains() Function
- CSS | hsl() Function
- p5.js | sin() function
- PHP | abs() Function
- p5.js | tan() function
- PHP | cos( ) Function
- PHP | Ds\Map map() Function
- p5.js | arc() Function
- PHP | sin( ) Function
- PHP | tan( ) Function
- PHP | Ds\Set last() Function
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.



