English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP unlink() Function Usage and Example

PHP Filesystem 参考手册

The unlink() function can delete a file, returning true on success and false on failure.

Syntax

bool unlink ( string $filename [, resource $context ] )

This function can delete the filename, similar to the Unix C unlink() function.

Example 1

<?php
   $file = "/PhpProject/php/sample.txt";
   if(!unlink($file)) {
      echo("Error occurred while deleting $file");
   } else {
      echo("Successfully deleted $file");
   }
?>

Output result

Successfully deleted /PhpProject/php/sample.txt

Example 2

<?php
   $fh = fopen("/PhpProject/test.html", "a");
   fwrite($fh, "<h1>Hello world!</h1>");
   fclose($fh);
   unlink("/PhpProject1/test.html");
?>

Output result

file deleted successfully

PHP Filesystem 参考手册