is_writable

(PHP 4, PHP 5, PHP 7)

is_writableSprawdza czy można zapisać do pliku

Opis

is_writable ( string $nazwa_pliku ) : bool

Zwraca TRUE jeżeli plik nazwa_pliku istnieje i jest zapisywalny. Jako ten argument można podać nazwę katalogu, aby sprawdzić czy katalog jest zapisywalny.

Miej świadomość, że PHP będzie próbował uzyskać dostęp do pliku jako użytkownik, pod którym jest uruchamiany serwer (często 'nobody'). Ograniczenia trybu bezpiecznego nie są brane pod uwagę.

Parametry

nazwa_pliku

Nazwa sprawdzanego pliku lub katalogu.

Zwracane wartości

Zwraca TRUE jeżeli nazwa_pliku istnieje i jest zapisywalny.

Przykłady

Przykład #1 Przykład użycia is_writable()

<?php
$filename 
'test.txt';
if (
is_writable($filename)) {
    echo 
'Plik jest zapisywalny';
} else {
    echo 
'Plik nie jest zapisywalny';
}
?>

Błędy/Wyjątki

W przypadku niepowodzenia jest emitowany E_WARNING.

Notatki

Informacja: Wyniki działania tej funkcji są buforowane. Zobacz opis funkcji clearstatcache() aby uzyskać więcej informacji.

Wskazówka

Od PHP 5.0.0 ta funkcja może być użyta także z niektórymi nakładkami URL. Zobacz Wspierane protokoły i wrappery aby dowiedzieć się które obsługują funkcjonalność z rodziny stat().

Zobacz też:

add a note add a note

User Contributed Notes 14 notes

up
14
helvete at bahno dot net
7 years ago
Be warned, that is_writable returns false for non-existent files, although they can be written to the queried path.
up
5
arikan134 at gmail dot com
8 years ago
Check director is writable recursively. to return true, all of directory contents  must be writable

<?php
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));
       
    }
}

?>
up
8
starrychloe at yahoo dot com
16 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
up
7
darek at fauxaddress dot com
18 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
up
2
JimmyNighthawk
18 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
up
3
agrenier at assertex dot com
19 years ago
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.

<?php
   
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;
        }
    }
?>
up
2
gr
13 years ago
The results of this function seems to be not cached :
Tested on linux and windows

<?php
chmod
($s_pathFichier, 0400);
echo
'<pre>';var_dump(is_writable($s_pathFichier));echo'</pre>';
chmod($s_pathFichier, 04600);
echo
'<pre>';var_dump(is_writable($s_pathFichier));echo'</pre>';
exit;
?>
up
0
greg at gregwhitescarver dotcalm
18 years ago
In response to Darek:

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.'
up
-1
legolas558 d0t users dot sf dot net
17 years ago
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.

<?php
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;
}
?>
up
-3
shkkmo at gmail dot com
9 years ago
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.
up
-3
samuel dot zallocco at univaq dot it
9 years ago
Check if a directory is writable. Work also on mounted SMB shares:

function isWritablePath($home, $xpath) {
    $isOK = false;
    $path = trim($xpath);
    if ( ($path!="") && (strpos($path,$home)!==false)  && is_dir($path) && is_writable($path) ) {
        $tmpfile = "mPC_".uniqid(mt_rand()).'.writable';
        $fullpathname = str_replace('//','/',$path."/".$tmpfile);
        $fp = @fopen($fullpathname,"w");
        if ($fp !== false) {
            $isOK = true;
        }
        @fclose($fp);
        @unlink($fullpathname);
    }
    return $isOK;
   
}
up
-10
yury dot gugnin at gmail dot com
10 years ago
function is_writable('ftp://user.....') always return false. I can create/delete files, but can check is writable. Is this bug or php feature :)?
up
-5
legolas558 dot sourceforge comma net
17 years ago
Since looks like the Windows ACLs bug "wont fix" (see http://bugs.php.net/bug.php?id=27609) I propose this alternative function:

<?php

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
up
-5
claude dot paroz at ne dot ch
19 years ago
Under Windows, it only returns the read-only attribute status, not the actual permissions (ACL).
See http://bugs.php.net/bug.php?id=27609
To Top