Deleting files with unlink()

To delete files, simply pass a filename string as the only parameter to unlink(). Note that unlink deals only with files - to delete directories you need rmdir().

    if (unlink($filename)) {
        print "Deleted $filename!\n";
    } else {
        print "Delete of $filename failed!\n";

Author's Note: if you have a file opened with fopen(), you need to fclose() it before you call unlink()


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Temporary files >>

Previous chapter: Copying files with copy()

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.