PHP: Hypertext Preprocessor (original) (raw)

is_writable

(PHP 4, PHP 5, PHP 7, PHP 8)

is_writable — Tells whether the filename is writable

Description

Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').

Parameters

filename

The filename being checked.

Return Values

Returns [true](reserved.constants.php#constant.true) if the filename exists and is writable.

Errors/Exceptions

Upon failure, an [E_WARNING](errorfunc.constants.php#constant.e-warning) is emitted.

Examples

Example #1 is_writable() example

<?php $filename = 'test.txt'; if (is_writable($filename)) { echo 'The file is writable'; } else { echo 'The file is not writable'; } ?>

Notes

Note: The results of this function are cached. See clearstatcache() for more details.

See Also

Found A Problem?

helvete at bahno dot net

8 years ago

Be warned, that is_writable returns false for non-existent files, although they can be written to the queried path.

h3ssan at protonmail dot com

7 months ago

In Linux, you might encountering an issue which is a file is not writable even tho it has 644 permission! The problem is with SELinux, just disable it or add rules to allow it.

starrychloe at yahoo dot com

17 years ago

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

arikan134 at gmail dot com

9 years ago

`Check director is writable recursively. to return true, all of directory contents must be writable

objects=scandir(objects = scandir(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)); } } ?>

`

darek at fauxaddress dot com

19 years ago

`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

`

JimmyNighthawk

19 years ago

`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

`

agrenier at assertex dot com

21 years ago

`This file_write() function will give filenamethewritepermissionbeforewritingfilename the write permission before writing filenamethewritepermissionbeforewritingcontent to it.

Note that many servers do not allow file permissions to be changed by the PHP user.

`

gr

14 years ago

`The results of this function seems to be not cached :
Tested on linux and windows

';var_dump(is_writable($s_pathFichier));echo''; chmod($s_pathFichier, 04600); echo'
';var_dump(is_writable($s_pathFichier));echo'
'; exit; ?>

`