PHP-File Delete

  • Now you know how to create, open or close a file and also you know how to read, write and append data from a file. Now its time to learn how to vanish or destroy a file.
  • In PHP all files are deleted or destroyed using unlink function.

PHP Unlink Function

  • Before you can delete (unlink) a file, you must first be sure that it is not open in your program. Use the fclose function to close down an open file.
  • Now we are going to delete the write.txt file that we have created in the Chapter File Write.
  • Now to delete write.txt file we have to only run a PHP script that is located in the same directory. To delete the file We just need to know the name of the file.

    $myFile = "write.txt";
    unlink($myFile);
  • Now the write.txt file has been removed from our directory.