PHP | unlink() Function (original) (raw)

Last Updated : 09 Apr, 2025

The unlink() function in PHP is used to delete a file from the filesystem. It does not delete directories. For that, you would need to use rmdir() or other methods.

**Syntax:

unlink( filename,filename, filename,context )

**In this syntax:

The unlink() function works in the following ways:

1. Deleting a File

Here’s a basic example of how to use the unlink() function to delete a file.

PHP `

`

**Output:

Screenshot-2025-04-09-181637

php unlink() function

**In this example:

2. Deleting an Uploaded File

If your application allows users to upload files and you need to delete a file after some time (for example, after processing), you can use unlink().

PHP `

`

**Output:

Screenshot-2025-04-09-181919

php unlink() function

**In this example:

Conclusion

The unlink() function in PHP is a straightforward and useful way to remove files from the filesystem. Whether you’re managing uploaded files, cleaning up temporary files, or simply performing file maintenance, unlink() provides an efficient means of deletion. However, it’s important to handle errors gracefully, check file permissions, and ensure that you’re deleting the correct files to avoid data loss.