touch
Creates an empty file or updates its modification time.
Syntax
touch(string $filename, ?int $mtime = null, ?int $atime = null): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $filename | string | The path to the file. |
| $mtime | ?int | Optional. The new modification time. |
| $atime | ?int | Optional. The new access time. |
Return value
bool`true` on success.
Examples
Creating an empty file:
<?php
$file = sys_get_temp_dir() . '/ref-touch.lock';
touch($file);
var_dump(file_exists($file), filesize($file));Output:
bool(true)
int(0)Notes
The contents of an existing file are untouched — only the time is updated.
See also
- filemtime — Returns the time a file was last modified.
- file_put_contents — Writes data to a file.
- file_exists — Checks whether a file or folder exists.
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.