Friday, July 1, 2016

PHP Unlink Function

Remember from the PHP File Create lesson that we created a file named testFile.txt.

PHP Code:

$myFile = "testFile.txt";
$fh = fopen($myFile, w) or die("cant open file");
fclose($fh);

Now to delete testFile.txt we simply run a PHP script that is located in the same directory. Unlink just needs to know the name of the file to start working its destructive magic.

PHP Code:

$myFile = "testFile.txt";
unlink($myFile);

The testFile.txt should now be removed.

PHP - Unlink: Safety First!

With great power comes a slough of potential things you can mess up! When you are performing the unlink function be sure that you are deleting the right file!