Hacking with PHP has been updated for PHP 7 - click here! >>

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()


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.