unlink
Deletes a file.
Syntax
unlink(string $filename): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $filename | string | The path to the file. |
Return value
bool`true` if the file was deleted.
Examples
Deleting a file:
<?php
$file = sys_get_temp_dir() . '/ref-delete.txt';
file_put_contents($file, 'temp');
var_dump(unlink($file));
var_dump(file_exists($file));Output:
bool(true)
bool(false)Notes
Deleting a missing file raises a warning — check file_exists() first. Folders are removed by another function, rmdir().
See also
- file_exists — Checks whether a file or folder exists.
- mkdir — Creates a folder.
Related recipes
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.