Hàm touch() thiết lập thời gian truy cập (access time) và sửa đổi (modification time) của file. Nếu file không tồn tại, sẽ tạo file mới rỗng. Trả về TRUE nếu thành công, FALSE nếu thất bại.
Cú pháp
touch(filename, time, atime)
Tham số
filename – Tên file.
time – Thời gian sửa đổi (Unix timestamp). Mặc định: thời gian hiện tại.
atime – Thời gian truy cập (Unix timestamp). Mặc định: time parameter hoặc thời gian hiện tại.
Giá trị trả về
Trả về TRUE nếu thành công, FALSE nếu thất bại.
Ví dụ 1: Cập nhật thời gian sửa đổi thành hiện tại
<?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.";
}
?>
Kết quả
The modification time of new.txt set to current time.
Ví dụ 2: Đặt thời gian sửa đổi 8 giờ trước
<?php
$myfile = "new.txt";
$set_time = time() - 28800; // 8 giờ = 28800 giây
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.";
}
?>
Kết quả
The modification time of new.txt updated to 8 hrs in the past.