PHP - Fonction unlink ()

La fonction unlink () peut supprimer un fichier, et elle peut retourner true en cas de succès ou false en cas d'échec.

Syntaxe

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

Cette fonction peut supprimer un nom de fichier, et elle est similaire à la fonction Unix C unlink ().

Exemple 1

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

Production

Deleted /PhpProject/php/sample.txt successfully

Exemple-2

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

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

Production

file deleted succcessfully