Home PHP Filesystem touch() PHP touch() Function Definition In PHP, touch() sets the modified time and access time of a file. If the file does not exist, it creates an empty one.
Called with only a file name, it uses the current time. You can also pass your own Unix timestamps, which is handy for tests, cache files and marking when something last ran.
Syntax Call touch() with the values you need. Start with this line.
$ok = touch('last-run.txt'); Call What it does touch('flag.txt')Creates an empty flag.txt, or updates its times to now if it exists. touch('flag.txt', 1700000000)Sets the modified time to a fixed Unix timestamp. touch('flag.txt', time(), time() - 3600)Sets the modified time to now and the access time to one hour ago.
If you pass only $mtime, PHP uses the same value for the access time. If you pass neither, both use the current time. After using touch(), call clearstatcache() before reading the time with filemtime().
Library declarations touch(string $filename, ?int $mtime = null, ?int $atime = null): bool Parameters Parameter Description filename Required. The path of the file to touch. It is created if it does not exist. mtime Optional. The modified time as a Unix timestamp. The default is the current time. atime Optional. The access time as a Unix timestamp. If left out, it uses the same value as mtime.
Return Value Return Value Description bool Returns true on success or false on failure.
PHP Examples 1. Create an Empty File 2. Set a Fixed Modified Time 3. Find Out If a File Is Old Example 1 — Create an Empty File Use touch() to create a new empty file.
<?php
$file = sys_get_temp_dir() . '/touch_demo.txt';
echo 'Before: ', file_exists($file) ? 'exists' : 'missing', PHP_EOL;
touch($file);
echo 'After: ', file_exists($file) ? 'exists' : 'missing', PHP_EOL;
echo 'Size: ', filesize($file), ' bytes', PHP_EOL;
unlink($file); Before: missing
After: exists
Size: 0 bytes How It Works file_exists() shows the file is not there yet. touch() creates it because it does not exist. The new file is empty, so filesize() reports 0 bytes. unlink() removes the test file. Example 2 — Set a Fixed Modified Time Pass a Unix timestamp to touch() and read it back with filemtime().
<?php
$file = sys_get_temp_dir() . '/touch_time.txt';
touch($file, 1700000000);
clearstatcache();
echo gmdate('Y-m-d H:i:s', filemtime($file)), PHP_EOL;
unlink($file); How It Works touch() sets the modified time to the timestamp 1700000000. clearstatcache() makes sure PHP does not return an older cached time. filemtime() reads the modified time as a number. gmdate() formats it as a UTC date, so the output is the same on every server. Frequently Asked Questions What does touch() do in PHP? It sets the access and modified time of a file. If the file does not exist, it creates a new empty file.
How do I create an empty file in PHP? Call touch('name.txt'). It creates the file if it is missing and leaves existing content unchanged.
Does touch() change the content of a file? No. It only changes the time stamps. Existing content stays exactly as it was.
How do I set a specific date with touch()? Pass a Unix timestamp as the second argument. You can build one with strtotime(), for example touch('a.txt', strtotime('2024-01-01')).
Why does filemtime() show the old time after touch()? PHP caches file information. Call clearstatcache() before reading the time again.
Next: umask() umask() reads or changes the default permission mask. The mask decides which permissions a.
umask() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion