1.basename()
2.copy()
3.unlink()
4.file_exists()
5.file_get_contents()
6.file_put_contents()
1.ฟังก์ชัน basename() ใช้ในการอ่านชื่อไฟล์จาก path ที่กำหนด
syntax : basename ($path, $suffix)
$path คือเก็บที่อยู่ของไฟล์ $suffixคือ นามสกุลของไฟล์
ตัวอย่าง
โค้ด: เลือกทั้งหมด
$path = "/test/t.txt";
echo "1)" . basename("$path") . "<br/>"; //กรณีที่ไม่มีการกำหนดตัวแปร public จะแสดงทั้งชื่อและนามสกุลของไฟล์
echo "2)" . basename("$path","txt") . "<br/>"; //กรณีมีการกำหนดตัวแปร public ขึ้นมาด้วย จะแสดงแค่ชื่อของไฟล์
syntax: copy ($file,$new_file)
file คือ ที่อยู่ของไฟล์ที่ต้องการคัดลอก
ืnew_file คือที่อยู่ของไฟล์ปลายทาง
ตัวอย่าง
โค้ด: เลือกทั้งหมด
$file = "test/t.txt";
$new_file = "test/new_t.txt";
if (copy("$file","$new_file")) {
echo "คัดลอกไฟล์เรียบร้อย";
} else {
echo ("คัดลอกไฟล์ไม่สำเร็จ");
}
syntax: unlink ($file)
$file คือที่อยู่ของไฟล์
ตัวอย่าง
โค้ด: เลือกทั้งหมด
$file = "test/new_t.txt";
if (unlink("$file")) {
echo ("ลบไฟล์เรียบร้อย $file");
} else {
echo ("ลบไฟล์ไม่สำเร็จ");
}
syntax: file_exists ($file)
$file คือที่อยู่ของไฟล์
ตัวอย่าง
โค้ด: เลือกทั้งหมด
$file = "test/t.txt";
if (file_exists($file)) {
echo ("file exists");
} else {
echo ("ไม่พบไฟล์");
}
5.ฟังก์ชัน file_get_contents() ใช้ในการอ่านค่าในไฟล์ออกมาเป็นข้อความ String
syntax : file_get_contents ($file)
$file คือ ที่อยู่ของไฟล์
ตัวอย่าง
โค้ด: เลือกทั้งหมด
$file = "test/t.txt";
echo file_get_contents($file);
syntax: file_put_contents ($file,$data)
$file คือที่อยู่ของไฟล์
$data คือข้อมูลที่เขียนลงไปในไฟล์
ตัวอย่าง
โค้ด: เลือกทั้งหมด
$file = "test/t.txt";
$data = "MIDEPHPVideoman";
echo file_put_contents($file,$data);