Hàm is_writable() sẽ kiểm tra xem file truyền vào có quyền ghi hay không.
is_writable ( string $filename ) : bool$filename là đường dẫn tới file cần kiểm tra.<?php
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'File có thể được ghi';
} else {
echo 'File không thể được ghi';
}
?><?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)); } } ?>
<?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;
}
}
?>Các bạn có thể xem chi tiết hơn trên php.net.
Hi vọng với bài viết này, bạn đã hiểu rõ ứng dụng của hàm is_writable() trong PHP. Nếu bạn thấy bài viết hay và có ý nghĩa hãy like và chia sẻ bài viết này để mọi người cùng nhau học tập nhé. Cảm ơn các bạn đã ghé thăm codetutam.com
Bình luận: