unlink()
unlink() — Deletes a file.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Deletes filename. Similar to the Unix C unlink() function. An E_WARNING level error will be generated on failure.
Signature
unlink(string $filename, ?resource $context = null): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | Path to the file. |
| $context | ?resource | A context stream resource. |
Return value
Returns true on success or false on failure.
Example
A runnable snippet:
$fh = fopen('test.html', 'a'); fwrite($fh, '<h1>Hello world!</h1>'); fclose($fh); unlink('test.html');
Changelog
| Version | Change |
|---|---|
| 7.3.0 | On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page unlink() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.