php判断文件名,PHP 判断给定的文件名是否可写

用户评论:

gr (2010-10-05 03:32:50)

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;?>

starrychloe at yahoo dot com (2008-02-10 05:50:18)

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 (2007-09-25 21:46:09)

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 (2007-03-02 12:18:11)

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.

//NOTE: use a trailing slash for folders!!!

//see http://bugs.php.net/bug.php?id=27609

//see http://bugs.php.net/bug.php?id=30931if ($path{strlen($path)-1}=='/')// recursively return a temporary file pathreturnis__writable($path.uniqid(mt_rand()).'.tmp');

else if (is_dir($path))

returnis__writable($path.'/'.uniqid(mt_rand()).'.tmp');// check tmp file for read/write capabilities$rm=file_exists($path);$f= @fopen($path,'a');

if ($f===false)

returnfalse;fclose($f);

if (!$rm)unlink($path);

returntrue;

}?>

haccel at email dot com (2006-08-03 17:51:42)

Ooooops, sorry! My mistake. is__writable should be:

{

if ($path{strlen($path)-1}=='/')//Start function again with tmp file...returnis__writable($path.uniqid(mt_rand()).'.tmp');

elseif (ereg('.tmp',$path))

{//Check tmp file for read/write capabilitiesif (!($f= @fopen($path,'w+')))

returnfalse;fclose($f);unlink($path);

returntrue;

}

else//We have a path error.return0;// 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 (2006-08-02 20:56:33)

Be careful of legolas558 dot sourceforge comma net's example, try this instead:

{

if ($path{strlen($path)-1}=='/')

returnis__writable($path.uniqid(mt_rand()).'.tmp');

elseif (file_exists($path) &&ereg('.tmp',$path))

{

if (!($f= @fopen($path,'w+')))

returnfalse;fclose($f);unlink($path);

returntrue;

}

else

return0;// Or return error - invalid path...}?>

legolas558 dot sourceforge comma net (2006-07-13 01:17:07)

Since looks like the Windows ACLs bug "wont fix" (see http://bugs.php.net/bug.php?id=27609) I propose this alternative function:

if ($path{strlen($path)-1}=='/')

returnis__writable($path.uniqid(mt_rand()).'.tmp');

if (file_exists($path)) {

if (!($f= @fopen($path,'r+')))

returnfalse;fclose($f);

returntrue;

}

if (!($f= @fopen($path,'w')))

returnfalse;fclose($f);unlink($path);

returntrue;

}?>

It should work both on *nix and Windows

NOTE: you must use a trailing slash to identify a directory

Nils Kuebler (2006-04-18 15:15:32)

this one recursivly checks if a folder and all its contents are writeable

{$folder=opendir($dir);

while($file=readdir($folder))

if($file!='.'&&$file!='..'&&

( !is_writable($dir."/".$file) ||

(is_dir($dir."/".$file) && !is_removeable($dir."/".$file)  ) ))

{closedir($dir);

returnfalse;

}closedir($dir);

returntrue;

}?>

greg at gregwhitescarver dotcalm (2006-02-07 06:55:04)

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 (2006-01-31 10:27:03)

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 (2005-09-12 02:02:08)

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 (2004-04-06 04:28:04)

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 (2004-04-02 04:56:18)

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.

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;

}

}?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值