Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ฟังก์ชัน is_writable() ใน PHP


ฟังก์ชัน is_writable() ใช้ ตรวจสอบว่าไฟล์เขียนได้ (writable) หรือไม่ ส่งคืน TRUE หากไฟล์มีอยู่และเขียนได้, FALSE หากไม่เขียนได้หรือไม่มีไฟล์

ไวยากรณ์ (Syntax)

is_writable(file_path)

พารามิเตอร์ (Parameters)

  • file_path − พาธของไฟล์ที่ต้องการตรวจสอบ

ค่าที่ส่งคืน (Return Value)

ส่งคืน TRUE หากไฟล์มีอยู่และเขียนได้, FALSE หากไม่เขียนได้หรือไม่มีไฟล์

ตัวอย่างการใช้งาน

ตัวอย่างที่ 1: ตรวจสอบไฟล์ทั่วไป

<?php
$file = "one.txt";
if (is_writable($file)) {
echo "File is writeable!";
} else {
echo "File is not writeable!";
}
?>

ผลลัพธ์

File is not writeable!

ตัวอย่างที่ 2: ตรวจสอบพร้อมแสดงสิทธิ์ไฟล์

<?php
$file_permissions = fileperms("two.txt");
$res = sprintf("%o", $file_permissions);
$file = "two.txt";
if (is_writable($file)) {
echo "File is writeable!";
echo "Permission = $res";
} else {
echo "File is not writeable!";
echo "Permission = $res";
}
?>

ผลลัพธ์

File is writeable! Permission: 0777