Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

unlink

Deletes a file.

Syntax

unlink(string $filename): bool

Parameters

ParameterTypeDescription
$filenamestringThe 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.

Thanks for reading!

Donate to the site — we work hard for you.