PHP Tutorial - PHP unlink() Function






Definition

The unlink() function deletes a file.

Syntax

PHP unlink() Function has the following syntax.

unlink(filename,context)

Parameter

ParameterIs RequiredDescription
filenameRequired.File to delete
contextOptional.Context of the file handle.

Return

This function returns TRUE on success, or FALSE on failure.





Example

Files are deleted using unlink().

To delete files, pass a filename string as the only parameter to unlink(). To delete directories, you need rmdir().


<?PHP
      $filename = "c:/abc/test.txt";
      if (unlink($filename)) {
             print "Deleted $filename!\n";
      } else {
             print "Delete of $filename failed!\n";
      }
?>

If you have a file opened with fopen(), you need to fclose() it before you call unlink().