Summary: in this tutorial, you will learn how to delete a file in PHP using the unlink() function. Show Introduction to the PHP delete file functionTo delete a file, you use the
The
The Let’s take some examples of using the unlink() function. 1) Simple PHP delete file exampleThe following example uses the unlink() function to delete the readme.txt file:
2) Delete all files in a directory that match a patternThe following example deletes all files with the .tmp extension:
How it works.
Generally, you can change the pattern to delete all matching files in a directory using the Summary
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
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 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. How do you empty a directory in PHP?The rmdir() function in PHP is an inbuilt function which is used to remove an empty directory. It is mandatory for the directory to be empty, and it must have the relevant permissions which are required to delete the directory.
How do I empty a folder?To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
How do you empty a file in PHP?To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.
How can I delete all files in a directory in PHP?Validate the file while checking if the file directory has a dot or not. Using the getPathName method reference, delete the file using unlink() method.. Generate a list of files using glob() method.. Iterate over the list of files.. Check whether the name of files is valid.. Delete the file using unlink() method.. |