Summary: in this tutorial, you will learn how to delete a file in PHP using the unlink() function.
Introduction to the PHP delete file function
To delete a file, you use the unlink() function:
unlink ( string $filename , resource $context = ? ) : bool
Code language: PHP (php)The unlink() function has two parameters:
- $filename is the full path to the file that you want to delete.
- $context is a valid context resource.
The unlink() function returns true if it deletes the file successfully or false otherwise. If the $filename doesn’t exist, the unlink() function also issues a warning and returns false.
Let’s take some examples of using the unlink() function.
1) Simple PHP delete file example
The following example uses the unlink() function to delete the readme.txt file:
<?php $filename = 'readme.txt'; if (unlink($filename)) { echo 'The file ' . $filename . ' was deleted successfully!'; } else { echo 'There was a error deleting the file ' . $filename; }
Code language: HTML, XML (xml)2) Delete all files in a directory that match a pattern
The following example deletes all files with the .tmp extension:
<?php $dir = 'temp/'; array_map('unlink', glob("{$dir}*.tmp"));
Code language: HTML, XML (xml)How it works.
- First, define a variable that stores the path to the directory in which you want to delete files.
- Second, use the glob() function to search for all files in the directory $dir that has the *.tmp extension and pass it result to the array_map() function to delete the files.
Generally, you can change the pattern to delete all matching files in a directory using the array_map(), unlink() and glob() functions.
Summary
- Use PHP unlink() function to delete a file.
Did you find this tutorial useful?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<?php | |
function EmptySubFolders($path) { | |
$empty = true; | |
foreach(glob($path.DIRECTORY_SEPARATOR."*") as $file) { | |
if (is_dir($file)) { | |
if (!EmptySubFolders($file)) { | |
$empty = false; | |
} | |
} | |
else { | |
$empty = false; | |
} | |
} | |
if($empty) { | |
touch($path."/empty-folder"); | |
} | |
return $empty; | |
} | |
EmptySubFolders('.'); | |
?> |
In this post, I will show you how you can delete all files and folders from a folder using PHP. This is pretty straight-forward, as we simply loop through the files and delete them.
First of all, we would like to show you how to delete only files from a folder.
<?php //The name of the folder. $folder = 'files'; //Get a list of all of the file names in the folder. $files = glob($folder . '/*'); //Loop through the file list. foreach($files as $file){ //Make sure that this is a file and not a directory. if(is_file($file)){ //Use the unlink function to delete the file. unlink($file); } }Earlier we have seen about deleting just files which are present in a directory. Now, What if you have sub directories with files inside the folder. In that case the above script won’t able to work. You’ll need to create recursive function to delete all files, sub-directories and parent directory altogether.
Simply deleting a folder with using php’s function rmdir() won’t work. It will throw some exceptions if you attempt to remove folder directly with files in it. So first of all you have to delete files one by one from each sub folder and then delete folders by removing parent folder.
So, here is the recursive function to delete files and sub folders from the folder.
<?php // delete all files and sub-folders from a folder function deleteAll($dir) { foreach(glob($dir . '/*') as $file) { if(is_dir($file)) deleteAll($file); else unlink($file); } rmdir($dir); } ?>This is the function which will help you out to delete folder and sub folders and also files. You can use this function by simply calling:
<?php deleteAll('folder_name'); ?>The function above basically checks to see if the $file variable represents a path to a file. If it does represent a path to a file, it deletes the file using the function unlink. However, if $file represents a directory, then it gets a list of all files in said directory before deleting each one. Finally, it removes the sub-directory itself by using PHP’s rmdir function.
You can use the above php script to delete files and sub-directories in a given folder programmatically. The script is relatively fast but it depends on the amount of files and folders to be deleted.