19.5. Deleting a File

Problem

You want to delete a file.

Solution

Use unlink( ) :

unlink($file) or die ("can't delete $file: $php_errormsg");

Discussion

The function unlink( ) is only able to delete files that the user of the PHP process is able to delete. If you’re having trouble getting unlink( ) to work, check the permissions on the file and how you’re running PHP.

See Also

Documentation on unlink( ) at http://www.php.net/unlink.

Get PHP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.