In PHP, the touch() function is commonly used to modify a file’s access time (atime) and modification time (mtime), or to create an empty file if it does not exist. The full syntax of this function is as follows:
bool touch(string $filename, int $mtime = time(), int $atime = $mtime)
From the function definition, we can see that touch() accepts three parameters:
$filename: The path of the file to operate on.
$mtime: Optional. Sets the file’s modification time (defaults to the current time).
$atime: Optional. Sets the file’s access time (defaults to the same value as $mtime).
This represents the file’s last modification time, expressed as a Unix timestamp (seconds since January 1, 1970). If the file exists, its mtime will be updated to this time; if it does not exist, a new file will be created with this time set.
This parameter sets the file’s last access time, also in Unix timestamp format. It can be different from $mtime to allow more precise control over the file’s metadata.
Below is an example demonstrating how to use these two parameters:
<?php
// Assume the file path to operate on
$filename = 'example.txt';
<p>// Set times: modification time as 2023-01-01 12:00:00<br>
$mtime = strtotime('2023-01-01 12:00:00');<br>
// Set access time as 2022-12-31 23:59:59<br>
$atime = strtotime('2022-12-31 23:59:59');</p>
<p>// Use touch() to set the file’s mtime and atime<br>
if (touch($filename, $mtime, $atime)) {<br>
echo "File times updated successfully";<br>
} else {<br>
echo "Failed to update file times";<br>
}<br>
?><br>
In this code, if example.txt does not exist, it will be created; then its modification time will be set to noon on January 1, 2023, and its access time to 11:59:59 PM on December 31, 2022.
Faking File Times: Useful in testing or specific business logic where file timestamps need to be set to a historical time.
Supporting Cache Mechanisms: Some caching systems rely on file modification times to determine whether to refresh content. Manually setting timestamps allows control over caching behavior.
Simulating User Access Logs: When building log simulation systems, setting atime can simulate user file access activity.
If your server has security settings enabled—such as access control based on file timestamps (e.g., backup scripts that only process recently modified files)—modifying file times may affect program behavior. Therefore, exercise caution when using touch() to avoid unintended side effects.
Suppose a user uploads an image and you want to fix its timestamp to December 31, 2024. You can do it like this:
<?php
$uploadPath = '/var/www/gitbox.net/uploads/user_photo.jpg';
$mtime = strtotime('2024-12-31 00:00:00');
<p>if (touch($uploadPath, $mtime)) {<br>
echo "Timestamp set successfully";<br>
}<br>
?><br>
The above code fixes the timestamp of an uploaded image on the gitbox.net site, which facilitates subsequent file management or system integration.
touch()’s second and third parameters provide developers with fine-grained control over file timestamps. Whether for log simulation, cache control, or file metadata manipulation, properly using these parameters offers greater flexibility. Mastering this skill will help you handle file system-related logic with greater ease.