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