What is PHP unlink() Function?
If you want to delete a file in your server, use PHP unlink() function.
Syntax:
unlink(filename, context)
Parameters:
The Function has 1 required parameter and 1 optional parameter-
filename (Required): The path to the file that is to be deleted. Check example 1.
context (Optional): A context stream resource.
Return Values:
The function returns-
- TRUE – if it deletes the file successfully.
- FALSE – if for any reason, it can’t delete the file.
Examples:
Example 1: Simple unlink() example –
<?php
unlink("file.txt");
?>
Explanation:
The unlink() function simply deletes the file file.txt.
Example 2: With conditional statement-
<?php
if (unlink('file.txt')) {
echo "File deleted successfully.";
} else {
echo "ERROR. File not deleted.";
}
?>
Explanation:
We use the conditional statement “if else” to check whether the unlink() function has successfully deleted the file or not.
Example 3: With file_exists() function-
<?php
$filename = "file.txt";
if(file_exists($filename)) {
if (unlink('file.txt')) {
echo "File deleted successfully.";
} else {
echo "ERROR. File not deleted.";
}
}else
echo "The file does not exist.";
?>
Explanation:
Before removing a file, we check whether the file exists in the directory using file_exists() function.
How to use unlink() in coding?
As the function returns a Boolean value TRUE/FALSE, you can use this using conditional statements like if statement, ternary operator. Depending on the result the conditional statement returns, your program flow progresses. Have a look at the code-
<?php
if (unlink('file.txt')) {
echo "File deleted successfully.";
} else {
echo "ERROR. File not deleted.";
}
?>
Practical Usages of unlink() Function:
There are lots of situations when you need this function to remove files-
- Use this function to remove your existing product pictures, profile picture, etc.
- You also use this function to remove existing session file when a user logged out.
- Your application may also create log files which may not be necessary after a certain period. Then, you need to remove those files.
- For many reasons, your web application may create temporary files such as unsuccessful uploads, duplicate uploads for any reason etc. Use this function to remove those unnecessary files.
- Your application may create cache files to improve performance. After a certain time, you need to delete these files to optimize server space.
Notes on unlink() Function:
- This function is similar to Unix C unlink() function.
- The unlink() function throws an warning if it fails to remove the file for any reason.
- This function removes one file at a time.
How to remove multiple files at a time?
- If you know the file names (and, stored in an array), you can run a loop to delete all the files or selected files.
<?php $files = ["1.txt", "2.txt", "3.txt", "4.txt", "5.txt"]; for($i=0; $i<count($files);$i++){ unlink($files[$i]); } ?>
- If you want to remove all the files of same extension in a directory, use “*.extension”. Replace file extension in place of “extension”. Look at the code snippet below. Here, glob function creates an array that contains all the files that ends with .txt extension. Then, array_filter() function applies the unlink() function to all the files. And the unlink() function deletes all those files.
<?php array_map('unlink', glob("path/to/dir/*.txt")); ?>
- If you want to delete all the files in a directory of any extension, use “*.*”. Here, the asterisk after the period selects any file extension and the asterisk before the period selects all the files of any name. look at the code snippet below-
<?php array_map('unlink', glob("path/to/dir/*.*")); ?>
Best practices on unlink() Function:
- Before removing the file, it is a good practice to check whether the exists in the directory. You can use file_exists() function for this purpose. Check example 3.
- It’s always better sanitize and validate user input If users request to remove files.
- It’s good practice to make the file path dynamic so that you can change the path easily when you needed like changing the folder name.
Caution:
- If the folder where the file is located has no write permission, the function can’t remove the file even though all the codes you write are correct. It is a common mistake that happens and kills enormous amount of time.
- Don’t use error control operator (the at sign @) before unlink() function to suppress error, better you fix the issue.
<?php @unlink("file.txt"); ?>
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP unlink() Function
unlink() function is a very useful function among other built-in PHP file system functions. As it is very common to change images, files in most web applications, you’ll have to use the function there.