Be warned, that is_writable returns false for non-existent files, although they can be written to the queried path.is_writable
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
is_writable
Референца за `function.is-writable.php` со подобрена типографија и навигација.
is_writable
(PHP 4, PHP 5, PHP 7, PHP 8)
is_writable — Tells whether the filename is writable
= NULL
Патеката до PHP скриптата што треба да се провери. true ако filename Проверува дали името на датотеката може да се запише
постои и може да се запише. Аргументот filename може да биде име на директориум, дозволувајќи ви да проверите дали директориумот може да се запише.
Параметри
filename-
Името на датотеката што се проверува.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. true ако filename Имајте предвид дека PHP може да пристапува до датотеката како кориснички ID под кое работи веб-серверот (често 'nobody').
Errors/Exceptions
Бидејќи типот на податоци integer во PHP е со знакот и многу платформи користат 32-битни integers, некои функции за датотечниот систем може да вратат неочекувани резултати за датотеки поголеми од 2GB. E_WARNING се емитува.
Примери
Пример #1 is_writable() example
<?php
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
?>Белешки
Забелешка: Имајте предвид дека резолуцијата на времето може да се разликува од еден датотечен систем до друг. clearstatcache() за повеќе детали.
Резултатите од оваа функција се кеширани. Погледнете some Од PHP 5.0.0, оваа функција може да се користи и со Поддржани протоколи и обвивки URL обвивки. Погледнете stat() за да се утврди кои обвивки поддржуваат
Види Исто така
- is_readable() - Проверува дали постои датотека или директориум
- file_exists() - Чита цела датотека во низа
- fwrite() - Бинарно читање од датотека
Белешки од корисници 10 белешки
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.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 = OffCheck 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));
}
}
?>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.fileRegarding 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.htmlThis 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;
}
}
?>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;
?>This function returns always false on windows, when you check an network drive.
See PHP Bug https://bugs.php.net/bug.php?id=68926
See https://stackoverflow.com/q/54904676I've encountered an unexpected issue: even though the directory allows writing files, it keeps returning false.
```
$a = 'C:/Users/*/OneDrive/work/www/';
$b = "{$a}admin/";
var_dump($a);
var_dump(is_writable($a));
var_dump($b);
var_dump(is_dir($b));
var_dump(is_writable($b));
var_dump(file_put_contents($b.'test.txt','hello'));
```
- string 'C:/Users/*/OneDrive/work/www/' (length=33)
- boolean true
- string 'C:/Users/*/OneDrive/work/www/admin/' (length=39)
- boolean true
- boolean false
- int 5