Delete a file from server using PHP is so easy. You just need to unlink the file using the default PHP “unlink()” function. This function will take the file name with a complete path and return the true or false. It will return true if delete is successful otherwise false. Show
Deleting a file from a directory using the PHP unlink function.If you want to delete the file from the front end, you need to pass the name of the file from the front end to the backend. Inside the PHP script(back-end), you need to catch the file name then pass the file name with the actual location in the unlink() function. Note: before delete, make sure the "file exists" or not using the file_exists() function. PHP check file exists or not.if(file_exists($location_with_file_name)){ echo "File Found"; }else{ echo "File Not Found"; } PHP File Delete using the unlink function$delete = unlink($location_with_file_name); if($delete){ echo "delete success"; }else{ echo "delete not success"; } PHP script to delete files on server$file_name = $_POST['file_name']; // file name from front end $location_with_image_name = "../upload/".$file_name; if(file_exists($location_with_image_name)){ $delete = unlink($location_with_image_name); if($delete){ echo "delete success"; }else{ echo "delete not success"; } } Note: Make sure you upload file with unique name. Let’s see what steps are necessary to be taken to delete files with this method. First of all, you should create a files list with the glob() method. The second step is the iteration over that list. Then, you should inspect whether its a file or not. And, finally, it’s time to delete the given file with the unlink() method. Here is an example:
Or you can choose to apply a short code for the glob() method. First, it is necessary to create a files’ list with the glob() method. The next step is implementing the array_filter() method. Then, it comes to mapping the list to unlink() method with array_map. Here is how to do that:
In the framework of this approach, it is required to create a list of files with DirectoryIterator. Then, iterate over it. Afterwards, you should validate the files during the process of testing whether the directory includes a dot. Finally, apply the getPathName method reference, deleting the file with unlink(). Here is an example:
In this snippet, we showed you the most common and efficient ways to delete files from the folder with PHP. Summary: in this tutorial, you will learn how to delete a file in PHP using the unlink() function. 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? Which command is used to delete a file in PHP?In PHP, we can delete any file using unlink() function. The unlink() function accepts one argument only: file name. It is similar to UNIX C unlink() function. PHP unlink() generates E_WARNING level error if file is not deleted.
What is delete function PHP?There is no delete() function in PHP. If you need to delete a file, look at the unlink() function.
How do I delete a file in HTML?Right-click the files again and choose "Delete." Click "Yes" to confirm the deletion. Repeat this process to delete all the HTML read-only files on the computer.
How can I delete all files in a directory in PHP?Iterate over the list of files. Check whether the name of files is valid. Delete the file using unlink() method.. Generate a list of files using glob() method.. Filter the list using array_filter() or array_merge() methods.. Map the list to unlink() method using array_map() method.. |