W3docs

unlink()

In PHP, the unlink() function is used to delete a file. It is a useful function for working with files in your PHP scripts. In this article, we will cover

Introduction

In PHP, the unlink() function is used to delete a file. It is a useful function for working with files in your PHP scripts. In this article, we will cover everything you need to know about the unlink() function, including its syntax, parameters, and examples of how it can be used.

The unlink() function in PHP is used to delete a file. It takes a single parameter, which is the name of the file to be deleted.

When you use unlink(), PHP deletes the specified file. This can be useful for working with files in your PHP scripts. Note that the PHP process requires appropriate directory write and file read permissions to successfully delete a file.

The syntax of the unlink() function is as follows:

Syntax of the unlink() Function

unlink($filename, $context = null)

Here, $filename is the name of the file to be deleted.

Parameters

  • $filename: The path to the file to be deleted.
  • $context (optional): A context resource.

Return Value Returns true on success or false on failure.

Let's take a look at an example of how the unlink() function can be used in PHP.

Example 1: Deleting a File

Example of Using unlink()

if (unlink('example.txt')) {
    echo "File deleted successfully.";
} else {
    echo "Failed to delete the file.";
}

This example deletes the file named example.txt using the unlink() function.

Conclusion

The unlink() function in PHP provides a straightforward way to remove files. We hope that this article has given you a better understanding of how unlink() works and how it can be safely used in your own projects.

Practice

Practice

What is the function of 'unlink()' in PHP?