is_writable
Returns true if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often ‘nobody’).
Parameters
The filename being checked.
Return Values
Returns true if the filename exists and is writable.
Errors/Exceptions
Upon failure, an E_WARNING is emitted.
Examples
Example #1 is_writable() example
$filename = ‘test.txt’ ;
if ( is_writable ( $filename )) echo ‘The file is writable’ ;
> else echo ‘The file is not writable’ ;
>
?>?php
Notes
Note: The results of this function are cached. See clearstatcache() for more details.
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
See Also
- is_readable() — Tells whether a file exists and is readable
- file_exists() — Checks whether a file or directory exists
- fwrite() — Binary-safe file write
User Contributed Notes 15 notes
Be warned, that is_writable returns false for non-existent files, although they can be written to the queried path.
To Darek and F Dot: About group permissions, there is this note in the php.ini file:
; By default, Safe Mode does a UID compare check when
; opening files. If you want to relax this to a GID compare,
; then turn on safe_mode_gid.
safe_mode_gid = Off
It appears that is_writable() does not check full permissions of a file to determine whether the current user can write to it. For example, with Apache running as user ‘www’, and a member of the group ‘wheel’, is_writable() returns false on a file like
-rwxrwxr-x root wheel /etc/some.file
Check director is writable recursively. to return true, all of directory contents must be writable
function is_writable_r ( $dir ) if ( is_dir ( $dir )) if( is_writable ( $dir )) $objects = scandir ( $dir );
foreach ( $objects as $object ) if ( $object != «.» && $object != «..» ) if (! is_writable_r ( $dir . «/» . $object )) return false ;
else continue;
>
>
return true ;
>else return false ;
>
>else if( file_exists ( $dir )) return ( is_writable ( $dir ));
This file_write() function will give $filename the write permission before writing $content to it.
Note that many servers do not allow file permissions to be changed by the PHP user.
function file_write ( $filename , & $content ) <
if (! is_writable ( $filename )) if (! chmod ( $filename , 0666 )) echo «Cannot change the mode of file ( $filename )» ;
exit;
>;
>
if (! $fp = @ fopen ( $filename , «w» )) echo «Cannot open file ( $filename )» ;
exit;
>
if ( fwrite ( $fp , $content ) === FALSE ) echo «Cannot write to file ( $filename )» ;
exit;
>
if (! fclose ( $fp )) echo «Cannot close file ( $filename )» ;
exit;
>
>
?>
Regarding you might recognize your files on your web contructed by your PHP-scripts are grouped as NOBODY you can avoid this problem by setting up an FTP-Connection («ftp_connect», «ftp_raw», etc.) and use methods like «ftp_fput» to create these [instead of giving out rights so you can use the usual «unsecure» way]. This will give the files created not the GROUP NOBODY — it will give out the GROUP your FTP-Connection via your FTP-Program uses, too.
Furthermore you might want to hash the password for the FTP-Connection — then check out:
http://dev.mysql.com/doc/mysql/en/Password_hashing.html
The results of this function seems to be not cached :
Tested on linux and windows
chmod ( $s_pathFichier , 0400 );
echo ‘
' ; var_dump ( is_writable ( $s_pathFichier ));echo '
‘ ;
chmod ( $s_pathFichier , 04600 );
echo ‘
' ; var_dump ( is_writable ( $s_pathFichier ));echo '
‘ ;
exit;
?>
This function returns always false on windows, when you check an network drive.
We have two servers: one running PHP 5.0.4 and Apache 1.3.33, the other running PHP 4.3.5 and Apache 1.3.27. The PHP 4 server exhibits the behavior you are describing, with is_writable() returning ‘false’ even though the www user is in the group that owns the file, but the PHP 5 server is returning ‘true.’
This is the latest version of is__writable() I could come up with.
It can accept files or folders, but folders should end with a trailing slash! The function attempts to actually write a file, so it will correctly return true when a file/folder can be written to when the user has ACL write access to it.
function is__writable ( $path ) //will work in despite of Windows ACLs bug
//NOTE: use a trailing slash for folders.
//see http://bugs.php.net/bug.php?id=27609
//see http://bugs.php.net/bug.php?id=30931
if ( $path < strlen ( $path )- 1 >== ‘/’ ) // recursively return a temporary file path
return is__writable ( $path . uniqid ( mt_rand ()). ‘.tmp’ );
else if ( is_dir ( $path ))
return is__writable ( $path . ‘/’ . uniqid ( mt_rand ()). ‘.tmp’ );
// check tmp file for read/write capabilities
$rm = file_exists ( $path );
$f = @ fopen ( $path , ‘a’ );
if ( $f === false )
return false ;
fclose ( $f );
if (! $rm )
unlink ( $path );
return true ;
>
?>
Since looks like the Windows ACLs bug «wont fix» (see http://bugs.php.net/bug.php?id=27609) I propose this alternative function:
function is__writable ( $path )
if ( $path < strlen ( $path )- 1 >== ‘/’ )
return is__writable ( $path . uniqid ( mt_rand ()). ‘.tmp’ );
if ( file_exists ( $path )) if (!( $f = @ fopen ( $path , ‘r+’ )))
return false ;
fclose ( $f );
return true ;
>
if (!( $f = @ fopen ( $path , ‘w’ )))
return false ;
fclose ( $f );
unlink ( $path );
return true ;
>
?>
It should work both on *nix and Windows
NOTE: you must use a trailing slash to identify a directory
function is_writable(‘ftp://user. ‘) always return false. I can create/delete files, but can check is writable. Is this bug or php feature :)?
I’d like to also clarify a point on this. Even if you see 777 permissions for the directly, you may need to check your ACL, since your server’s group might not have write permissions there.
Check if a directory is writable. Work also on mounted SMB shares:
fileperms
Returns the file’s permissions as a numeric mode. Lower bits of this mode are the same as the permissions expected by chmod() , however on most platforms the return value will also include information on the type of file given as filename . The examples below demonstrate how to test the return value for specific permissions and file types on POSIX systems, including Linux and macOS.
For local files, the specific return value is that of the st_mode member of the structure returned by the C library’s stat() function. Exactly which bits are set can vary from platform to platform, and looking up your specific platform’s documentation is recommended if parsing the non-permission bits of the return value is required.
Returns false on failure.
Errors/Exceptions
Upon failure, an E_WARNING is emitted.
Examples
Example #1 Display permissions as an octal value
echo substr ( sprintf ( ‘%o’ , fileperms ( ‘/tmp’ )), — 4 );
echo substr ( sprintf ( ‘%o’ , fileperms ( ‘/etc/passwd’ )), — 4 );
?>?php
The above example will output:
Example #2 Display full permissions
switch ( $perms & 0xF000 ) case 0xC000 : // socket
$info = ‘s’ ;
break;
case 0xA000 : // symbolic link
$info = ‘l’ ;
break;
case 0x8000 : // regular
$info = ‘r’ ;
break;
case 0x6000 : // block special
$info = ‘b’ ;
break;
case 0x4000 : // directory
$info = ‘d’ ;
break;
case 0x2000 : // character special
$info = ‘c’ ;
break;
case 0x1000 : // FIFO pipe
$info = ‘p’ ;
break;
default: // unknown
$info = ‘u’ ;
>
The above example will output:
Notes
Note: The results of this function are cached. See clearstatcache() for more details.
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
See Also
- chmod() — Changes file mode
- is_readable() — Tells whether a file exists and is readable
- stat() — Gives information about a file
User Contributed Notes 9 notes
Don’t use substr, use bit operator
decoct ( fileperms ( $file ) & 0777 ); // return «755» for example
?>
If you want to compare permission
0755 === ( fileperms ( $file ) & 0777 );
?>
This may not be immediately apparent to some, but you can use octdec( $octal_value ) to match the permissions retrieved by file perms
//assumes file has 2770 permissions
$perm = fileperms ( __FILE__ );
$bit = «102770» ;
printf ( «%s\n» , octdec ( $bit ) );
printf ( «%s\n» , $perm );
An easy way to calculate fileperms to chmod is this:
Displays 666 or 777 (depends on chmod set).
Displays 0666 or 0777 and refers immediately to the number set with chmod();
Windows has a very different file permission model to Unix and integrates them only minimally.
Here’s how Windows calculates the bitmask.
u+w/g+w/o+w is set based on whether the file has the read only flag.
u+x/g+x/o+x is set based on whether $filename is an inherently executable file (e.g. bat) or a directory.
Windows isn’t integrating its ACLs at all.
Here is a small function I made : http://pastebin.com/iKky8Vtu
I was bored and I thought it could be useful.
mixed mkperms( string $perms [, bool return_as_string = false [, string $filename ] ] )
Returns permissions given a string in literal format and a filename.
If the file name is omitted, the permissions that the function will return are based on 000-permissions.
If return_as_string is set to true, the result will be output as a 644 format string. Otherwise it will return a string converted to base-10 for chmod.
echo mkperms ( ‘u+r’ , true ), «\n» ; // 400
echo mkperms ( ‘u+rwx,g+rw,o+x’ , true ), «\n» ; // 761
touch ( ‘myfile.txt’ ); // Create a file with any permissions
chmod ( ‘myfile.txt’ , mkperms ( ‘u=rwx,g=x,o=rw’ )); // myfile.txt is now at -rwx—xrw-
// Make a file and give it full permissions
touch ( ‘somefile.txt’ );
chmod ( ‘somefile.txt’ , 0777 );
echo mkperms ( ‘g-w,o-rw’ , true , ‘somefile.txt’ ); // 751
echo mkperms ( ‘u=rwx,g-r,o=-‘ , true , ‘somefile.txt’ ); // 730
// This way you can apply permissions to files
chmod ( ‘somefile.txt’ , mkperms ( ‘u=rwx,g-r,o=-‘ , false , ‘somefile.txt’ )); // somefile.txt is now at -rwx-wx—
?>
PS : sorry I had to put it on pastebin, or else it just made the note way too long.
A small function for the last 3 digits (777/755 ect.)
function getFilePermission ( $file ) $length = strlen ( decoct ( fileperms ( $file )))- 3 ;
return substr ( decoct ( fileperms ( $file )), $length );
>
?>
Since the output of decoct( fileperms(‘.’) ) is of the form: 40644
It seems the previous example is wrong, instead you should understand:
To get permissions formatted as «644»:
echo substr ( decoct ( fileperms ( ‘.’ ) ), 2 );
?>
To get permissions formatted as «0644»:
echo substr ( decoct ( fileperms ( ‘.’ ) ), 1 );
?>
On Linux (not tested on Windows), if you want a chmod-like permissions, you can use this function:
function file_perms ( $file , $octal = false )
if(! file_exists ( $file )) return false ;
return substr ( decoct ( $perms ), $cut );
>
?>
Using it:
$ touch foo.bar
$ chmod 0754 foo.bar
echo file_perms ( ‘foo.bar’ ); // prints: 754
echo file_perms ( ‘foo.bar’ , true ); // prints 0754
?>