ฟังก์ชัน touch() ใช้ ตั้งค่าเวลาการเข้าถึง (access time) และเวลาการแก้ไข (modification time) ของไฟล์ หากไฟล์ไม่มีอยู่ จะสร้างไฟล์ใหม่ว่างเปล่า ส่งคืน TRUE เมื่อสำเร็จ, FALSE เมื่อล้มเหลว
ไวยากรณ์ (Syntax)
touch(filename, time, atime)
พารามิเตอร์ (Parameters)
filename − ชื่อไฟล์ที่ต้องการตั้งค่าเวลา
time − เวลาการแก้ไข (modification time) เริ่มต้นคือเวลาปัจจุบัน
atime − เวลาการเข้าถึง (access time) เริ่มต้นคือเวลาปัจจุบัน
ค่าที่ส่งคืน (Return Value)
ส่งคืน TRUE เมื่อสำเร็จ, FALSE เมื่อล้มเหลว
ตัวอย่างการใช้งาน
ตัวอย่างที่ 1: ตั้งค่าเวลาแก้ไขเป็นเวลาปัจจุบัน
<?php
$myfile = "new.txt";
if (touch($myfile)) {
echo "The modification time of $myfile set to current time.";
} else {
echo "The modification time of $myfile can't be updated.";
}
?>
ผลลัพธ์
The modification time of new.txt set to current time.
ตัวอย่างที่ 2: ตั้งค่าเวลาแก้ไขเป็น 8 ชั่วโมงที่แล้ว
<?php
$myfile = "new.txt";
$set_time = time() - 28800; // 8 ชั่วโมง = 28800 วินาที
if (touch($myfile, $set_time)) {
echo "The modification time of $myfile updated to 8 hrs in the past.";
} else {
echo "The modification time of $myfile can't be updated.";
}
?>
ผลลัพธ์
The modification time of new.txt updated to 8 hrs in the past.