is_writable

(PHP 4, PHP 5)

is_writable -- Определяет, доступен ли файл для записи

Описание

bool is_writable ( string filename )

Возвращает TRUE, если файл filename существует и доступен для записи. Аргумент filename может быть именем директории, что позволяет вам проверять директории на доступность для записи.

Не забывайте, что PHP может обращаться к файлу от имени того пользователя, от которого запущен веб-сервер (обычно 'nobody'). Ограничения безопасного режима не принимаются во внимание.

Замечание: Результаты этой функции кэшируются. Более подробную информацию смотрите в разделе clearstatcache().

Подсказка: Начиная с PHP 5.0.0, эта функция также может быть использована с некоторыми упаковщиками url. Список упаковщиков, поддерживаемых семейством функций stat(), смотрите в Прил. M.

См. также описание функций is_readable(), file_exists() и fwrite().



is_writable
legolas558 dot sourceforge comma net
13-Jul-2006 01: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
24-May-2006 10:33
Same here with PHP 5.1.4. is_writable ignores my ACLs and also groups dont work sometimes.
Nils Kuebler
18-Apr-2006 03: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 06: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
31-Jan-2006 10: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 02: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 04: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 04: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_uploaded_fileis_writeable>
 Last updated: Mon, 14 Nov 2005