• PHP Video Tutorials

PHP - Function unlink()



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

Syntax

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

This function can delete a filename, and it is similar to Unix C unlink() function.

Example-1

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

Output

Deleted /PhpProject/php/sample.txt successfully

Example-2

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

   unlink("/PhpProject1/test.html");
?>

Output

file deleted succcessfully 
php_function_reference.htm
Advertisements