Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: is_writable - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

is_writeable" width="11" height="7"/> <is_uploaded_file
Last updated: Sun, 25 Nov 2007

view this page in

is_writable

(PHP 4, PHP 5)

is_writable — ファイルが書き込み可能かどうかを調べる

説明

bool is_writable ( string $filename )

filename が存在して、かつそれが書き込み可能であれば TRUEを返します。引数filenameはディレクトリ名とすることができ、 ディレクトリが書き込み可能であることを調べることが可能です。

PHP は、Web サーバが実行されているユーザ ID('nobody' が多い) でファイルにアクセスすることを覚えておいてください。 セーフモードの制限は働きません。

パラメータ

filename

調べたいファイル名。

返り値

filename が存在して書き込み可能な場合に TRUE を返します。

Example#1 is_writable() の例

<?php
$filename 
'test.txt';
if (
is_writable($filename)) {
    echo 
'このファイルは書き込み可能です';
} else {
    echo 
'このファイルは書き込みできません';
}
?>

注意

注意: この関数の結果は キャッシュされます。詳細は、clearstatcache() を参照してください。

ヒント

PHP 5.0.0 以降、この関数は、 何らかの URL ラッパーと組合せて使用することができます。 どのラッパーが stat() ファミリーをサポートしているか のリストについては、サポートされるプロトコル/ラッパー を参照してください。



is_writeable" width="11" height="7"/> <is_uploaded_file
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
is_writable
starrychloe at yahoo dot com
10-Feb-2008 10:50
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
wuhai
26-Sep-2007 01:46
I was trying to create a new file using fwrite, and i had the following error.  Any idea?
Warning: fwrite(): supplied argument is not a valid stream resource in
legolas558 d0t users dot sf dot net
03-Mar-2007 05:18
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;
}
?>
haccel at email dot com
04-Aug-2006 09:51
Ooooops, sorry! My mistake. is__writable should be:

<?php

function is__writable($path)
{

   if (
$path{strlen($path)-1}=='/') //Start function again with tmp file...
     
      
return is__writable($path.uniqid(mt_rand()).'.tmp');
 
   elseif (
ereg('.tmp', $path))
   {
//Check tmp file for read/write capabilities
     
      
if (!($f = @fopen($path, 'w+')))
           return
false;
      
fclose($f);
      
unlink($path);
       return
true;

   }
   else
//We have a path error.
     
      
return 0; // Or return error - invalid path...

}

?>

The original could've deleted a folder if the path was invalid  to start with (no trailing slash..) and the folder was writable to begin with...
haccel at email dot com
03-Aug-2006 12:56
Be careful of legolas558 dot sourceforge comma net's example, try this instead:

<?php

function is__writable($path)
{

    if (
$path{strlen($path)-1}=='/')
       
        return
is__writable($path.uniqid(mt_rand()).'.tmp');
   
    elseif (
file_exists($path) && ereg('.tmp', $path))
    {
       
        if (!(
$f = @fopen($path, 'w+')))
            return
false;
       
fclose($f);
       
unlink($path);
        return
true;

    }
    else
       
        return
0; // Or return error - invalid path...

}

?>
legolas558 dot sourceforge comma net
13-Jul-2006 05:17
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
f dot knodt at yotaweb dot de
25-May-2006 02:33
Same here with PHP 5.1.4. is_writable ignores my ACLs and also groups dont work sometimes.
Nils Kuebler
19-Apr-2006 07:15
this one recursivly checks if a folder and all its contents are writeable

<?php
function is_removeable($dir)
{
  
$folder = opendir($dir);
   while(
$file = readdir( $folder ))
    if(
$file != '.' && $file != '..' &&
        ( !
is_writable$dir."/".$file  ) ||
        ( 
is_dir(   $dir."/".$file   ) && !is_removeable(   $dir."/".$file   )  ) ))
   {
   
closedir($dir);
    return
false;
   }
  
closedir($dir);
   return
true;
}
?>
greg at gregwhitescarver dotcalm
07-Feb-2006 11:55
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.'
darek at fauxaddress dot com
01-Feb-2006 03:27
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
12-Sep-2005 06:02
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
claude dot paroz at ne dot ch
06-Apr-2004 08:28
Under Windows, it only returns the read-only attribute status, not the actual permissions (ACL).
See http://bugs.php.net/bug.php?id=27609
agrenier at assertex dot com
02-Apr-2004 09:56
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;
        }
    }
?>

is_writeable" width="11" height="7"/> <is_uploaded_file
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites