How to Delete a File Using PHP

PHP



In this article, we show how to delete a file using PHP.

Deleting files may be necessary for many reasons.

Deleting a file means that you completely erase it from a directory, so that the file no longer exists.

So how is this done with PHP?

PHP has a unlink() function that allows us to delete a file.

So the PHP code to delete a file is shown below.




So the code above is very simple. It's a single line.

The PHP delete() function takes in a single parameter.

The parameter is the file that we want to delete. In this parameter, we must specify the file name along with its file extension. Do not leave off the file extension, or else the function will not work.

So this is all that is required to delete a file.

Even though the code is above is very effective and does the job of deleting a file, many times we would want to add more to it, so that we can get more information. For example, does the file we want to delete exist? If the file does exist, was it successfully deleted?

The PHP code below deletes a file and gives us more information.



This code now is more forthwise. It tells us more.

We first see whether the file exists. If the file exists, the code advances to the next line. In this line, we create a variable named $deleted. This variable holds the result of the PHP unlink function. If the file has been deleted successfully, the $deleted variable will hold a 1 or TRUE value. If it hasn't, the $deleted variable will hold a 0 or FALSE value. If the file has been deleted successfully, the line "The file has been successfully deleted" is output. If not, the line "The file has not been successfully deleted" is output.

If the file doesn't exist, the line "The original file that you want to delete doesn't exist" is output.

So this code above tells us everything. It tells us whether the file we want to delete exists. And it tells us whether the file delete was successful or not.

And this is all that is required to delete a file using PHP.


How to Delete All Files in a Directory with a Certain Criterion or Criteria

The above code is great and works well. But the true beauty of this PHP delete() function, if you want to tap into the true beauty of it, is that it can delete all files in a directory matching a certain criterion or certain criteria.

Say, you have a list of files that have the word, "HTML4", such as "How-to-create-a-horizontal-line-using-HTML4", "How-to-create-a-paragraph-using-HTML4", and "How-to-create-an-anchor-tag-using-HTML4" and you've updated yourself to all HTML5 and you've decided you want to delete all files with HTML4.

Imagine if you have 70 of these files. Imagine if you have hundreds. You would have to go through each of these files and manually delete them.

This is the beauty of the PHP unlink() function.

With a single block of code, we can delete all of those files in less than a minute, instead of it taking hours or days with us manually doing it.

So how do you loop through all the files in a directory and delete the files we want to?



So this code above searches the Articles directory. Of course you can set this to any direcory you want on your website.

The PHP code checks to see if this is a directory. If so, it opens the directory. The while loop loops through all the files in the directory.

We then write an if statement to see whether each file contains the phrase "HTML4". If it does, we delete the file through the PHP unlink() function.

This is the beauty of programming and the PHP delete() function.


Related Resources

How to Read the Contents of a File Using PHP

How to Write Contents to a File Using PHP

How to Copy a File Using PHP

How to Rename a File Using PHP

How to List All Files in a Directory using PHP





HTML Comment Box is loading comments...