PHP Delete File

« Previous Tutorial Next Tutorial »

To delete any file in PHP, use unlink() function.

PHP Delete File Syntax

Here is the general form of deleting a file in PHP.


PHP Delete File Example

Before delete any file in PHP, let's first see the screenshot of C:\xampp\htdocs\ directory folder. Below is the screenshot of this directory.

php delete file

As you can see, there is a file named codescracker is present inside the current directory which is a textual file.

Now, here is an example of deleting a file in PHP

   <title>Deleting a file in PHP</title>
   $file_name = "codescracker.txt";
   echo "<p>Deleting the file <b>".$file_name."</b>.....</p>";
   echo "<p>File deleted successfully.</p>";

Here is the sample output produced by the above deleting file example code in PHP.

deleting a file php

And here is the screenshot of the current directory folder.

php delete file example

As you can see, the file codescracker.txt is deleted from the current directory.

PHP Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.