Fungsi touch
Fungsi touch memungkinkan mengubah waktu akses terakhir dan modifikasi file. Jika file yang ditentukan tidak ada, touch dapat membuatnya (secara default). Waktu dapat ditentukan secara eksplisit atau menggunakan waktu saat ini.
Sintaks
touch(string $filename, int $time = null, int $atime = null): bool
Contoh
Pembuatan file baru dengan waktu saat ini:
<?php
$file = 'newfile.txt';
if (touch($file)) {
echo "File $file dibuat";
}
?>
Hasil eksekusi kode:
"File newfile.txt dibuat"
Contoh
Mengubah waktu file yang sudah ada:
<?php
$file = 'existing.txt';
$time = strtotime('2023-01-01 12:00:00');
if (touch($file, $time)) {
echo "Waktu file $file diubah";
}
?>
Hasil eksekusi kode:
"Waktu file existing.txt diubah"
Contoh
Mengubah waktu akses dan modifikasi secara terpisah:
<?php
$file = 'test.txt';
$mtime = strtotime('2023-01-01 12:00:00');
$atime = strtotime('2023-01-02 12:00:00');
if (touch($file, $mtime, $atime)) {
echo "Stempel waktu file diperbarui";
}
?>