Syntax:
The syntax of the unlink() function is given below.
- The first argument of this function is mandatory and takes the filename removed.
- The second argument of this function is optional, which is used to set the file handler’s context and change the stream’s behavior. The default value of this argument is null.
- It returns True if the file deletes successfully; otherwise, it returns False.
unlink() Function Examples
The uses of the unlink() function have been shown in the next part of this tutorial by using multiple examples.
Example-1: Delete an Existing File
Create a PHP file with the following script to delete the temp.txt file if the file exists. The file_exists() function has been used in the script to check whether the file exists or not. If the file exists in the current location, the file will be deleted using the unlink() function. An error message will be displayed if the file does not exist in the current location.
//Assign the filename
$filename = "temp.txt";
//Check the existence of the file
if(file_exists($filename))
{
//Delete the file
if (!unlink($filename)) {
echo ("<br />Error occurs while deleting the $filename file.");
}
else {
echo ("</br>The <b>$filename</b> file has been deleted.");
}
}
else
echo "<br />The <b>$filename</b> file does not exist.";
?>
Output:
The following output would appear after executing the previous script if the file was in the current location:
Example-2: Delete the File After Creation
After creating the file, create a PHP file with the following script to delete the text.txt file. The file_exists() function has been used in the script to check whether the file has been created. If the file exists in the current location, the file will be deleted using the unlink() function. An error message will be displayed if the file does not exist.
//Assign the filename
$filename = "text.txt";
//Open file for writing
$fh = fopen($filename, 'w+');
//Write some text in the file
fwrite($fh, 'Welcome to linuxhint.');
fclose($fh);
//Check the existence of the file
if(file_exists($filename))
{
echo "<br />The <b>$filename</b> file exists.";
//Delete the file
if(!unlink($filename)) {
echo ("<br />Error occurs while deleting the $filename file.");
}
else {
echo ("<br />The <b>$filename</b> file has been deleted.");
}
}
else
echo "<br />The <b>$filename</b> file does not exist.";
?>
Output:
After executing the previous script, the following output would appear if the file was created and deleted properly:
Example-3: Delete All Files of the Particular Extension
Create a PHP file with the following script to delete all files of the “.txt” extension. The name and size of each text file will be printed before deleting the file.
//Search all text files in the current location
foreach (glob("*.txt") as $filename)
{
//Print the filename and the size
echo "<br />The filename is $filename";
echo "<br />The size of the file is ".filesize($filename)." bytes.";
//Delete the file
if (!unlink($filename))
echo ("<br />Error occurs while deleting the $filename file.");
else
echo ("<br />The <b>$filename</b> file has been deleted.");
}
Output:
The following similar output will appear after executing the previous script. The following output shows that three text files were deleted from the current location.
Example-4: Delete All Files From the Particular Directory
Create a PHP file with the following script to delete all files from the location, “temp/images”. The name and size of each file will be printed before deleting the file, like in the previous example. The string “*.*” has been used in the script to denote all files of all types of extensions.
//Set the directory path
$dir_path = "temp/images";
//Search all files in the directory
foreach (glob($dir_path."/*.*") as $filename)
{
//Print the filename and the size
echo "<br />The filename is <b>$filename</b>";
echo "<br />The size of the file is <b>".filesize($filename)."</b> bytes.";
//Delete the file
if (!unlink($filename))
echo ("<br />Error occurs while deleting the <b>$filename</b> file.");
else
echo ("<br />The <b>$filename</b> file has been deleted.");
}
Output:
The following similar output will appear after executing the previous script. The output shows that only one file was deleted from the current location.
Example-5: Delete All Files and Folders From the Directory
A directory may contain one or more sub-directories or folders and files. All files and folders are required to remove before deleting any directory. Create a PHP file with the following script to delete a directory containing one or more files and folders. The user-defined function named removeDir() has been used in the script to remove all files and folders of the temp directory and make it empty. The temp directory will be removed when it becomes empty.
//Set the directory path
$dir_path = "temp";
//Define function to delete the directory recursively
function removeDir($path) {
//Check for the subdirectory
$files = glob($path . '/*');
//Check the value is file or folder
foreach ($files as $file) {
is_dir($file) ? removeDir($file) : unlink($file);
}
//Remove the directory
rmdir($path);
//Return True
return1;
}
//Check the returned value of the function
if(removeDir($dir_path))
echo "All files and folders including $dir_path directory has been deleted.";
else
echo "Error occurred at the time of deletion.";
?>
Output:
The following output will appear after executing the previous script if the temp directory exists in the current location.
Conclusion
Different ways of using the unlink() function for deleting files have been shown in the examples of this tutorial to help PHP users use this function properly. Check the other Linux Hint articles for more tips and tutorials.