Home » Programming » PHP » Php Delete Unlink File

Deleting Files in PHP with unlink() [Examples]

This article will show you how to delete a single file or multiple files in PHP using the unlink() function, and provide code examples.

Once you have file uploads working in PHP, you may want to allow your users to delete files they no longer wish to keep. Read on to find out how.

There is no PHP Function called ‘Delete’

To clear up any confusion, unlink() is the function for deleting files in PHP. There is no built in PHP function called delete().

If you’re looking to delete a directory in PHP, check out the rmdir() function.

unlink() PHP Function Syntax

The syntax for the unlink() function used to delete files in PHP is as follows:

unlink($FILENAME, $CONTEXT)

Note that:

  • $FILENAME should be the path to the file you wish to be deleted
    • It can be a path relative to the running PHP script, or a system path
    • If the path specified is a soft (symbolic link), the link will be deleted, not the file it links to
  • $CONTEXT is an optional parameter which can be used to specify a context stream
    • This option is rarely used, you can probably ignore it
  • unlink() will return a boolean value
    • TRUE if successful, FALSE if not successful

Deleting a Single File

Here’s how to delete a single file with the unlink() function in PHP.

Ensure The File is Closed Before Deleting

First, if you have been using the file in your PHP script, you must ensure it is closed, otherwise the delete operation will fail.

// Close an open file pointer
fclose($filePointer);

Deleting the File from the Disk

If there are no open references to the file (or the file was never opened in the first place), it can be deleted with the unlink() function by specifying a path:

// Then unlink the file by its path
$pathToFile = 'path/to/file.txt';
unlink($pathToFile);

Using realpath() to Fix Path Issues

If you run into trouble with your paths (due to them containing symbolic links, containing extra slashes or because they are relative paths which do not resolve properly), you can try using the realpath() function to delete the file using the absolute path:

unlink(realpath($pathToFile));

Deleting Multiple Files Matching a Pattern

Multiple files can be deleted by using the glob() function to search for all files with names matching a certain pattern. Below, all files with the .txt file extension are found and then removed:

foreach (glob("*.txt") as $pathToFile) {
    unlink($pathToFile);
}

This code can be shortened by using array_map():

array_map('unlink', glob("path/to/files/*.txt"));

The unlink() function is called by it’s function name, while array_map() loops over the array results provided by glob().

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment