PHP touch() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

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.

PHP
$ok = touch('last-run.txt');
CallWhat 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
PHP
touch(string $filename, ?int $mtime = null, ?int $atime = null): bool

Parameters

ParameterDescription
filenameRequired. The path of the file to touch. It is created if it does not exist.
mtimeOptional. The modified time as a Unix timestamp. The default is the current time.
atimeOptional. The access time as a Unix timestamp. If left out, it uses the same value as mtime.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Create an Empty File

Use touch() to create a new empty file.

PHP
<?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);

How It Works

  1. file_exists() shows the file is not there yet.
  2. touch() creates it because it does not exist.
  3. The new file is empty, so filesize() reports 0 bytes.
  4. 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
<?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

  1. touch() sets the modified time to the timestamp 1700000000.
  2. clearstatcache() makes sure PHP does not return an older cached time.
  3. filemtime() reads the modified time as a number.
  4. gmdate() formats it as a UTC date, so the output is the same on every server.

Example 3 — Find Out If a File Is Old

Make a file look three days old with touch() and then test its age.

PHP
<?php
$file = sys_get_temp_dir() . '/touch_old.txt';

touch($file, time() - 3 * 86400);
clearstatcache();

$age = time() - filemtime($file);

echo 'Older than 1 day: ', $age > 86400 ? 'yes' : 'no', PHP_EOL;

touch($file);
clearstatcache();

$age = time() - filemtime($file);

echo 'Older than 1 day after touch: ', $age > 86400 ? 'yes' : 'no', PHP_EOL;

unlink($file);

How It Works

  1. 86400 is the number of seconds in one day, so 3 * 86400 is three days.
  2. touch() sets the modified time to three days ago and the age check says yes.
  3. A second touch() with no timestamp resets the time to now.
  4. The age is now only a few seconds, so the check says no. Cache cleanup scripts use this idea.

Frequently Asked Questions

It sets the access and modified time of a file. If the file does not exist, it creates a new empty file.
Call touch('name.txt'). It creates the file if it is missing and leaves existing content unchanged.
No. It only changes the time stamps. Existing content stays exactly as it was.
Pass a Unix timestamp as the second argument. You can build one with strtotime(), for example touch('a.txt', strtotime('2024-01-01')).
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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