How to delete a file using PHP ? Last Updated : 28 Mar, 2022 Comments Improve Suggest changes Like Article Like Report To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename, $context ); Below programs illustrate the above approach: Program 1: This program uses unlink() function to remove file from directory. Suppose there is a file named as "gfg.txt" php <?php // PHP program to delete a file named gfg.txt // using unlink() function $file_pointer = "gfg.txt"; // Use unlink() function to delete a file if (!unlink($file_pointer)) { echo ("$file_pointer cannot be deleted due to an error"); } else { echo ("$file_pointer has been deleted"); } ?> Output: gfg.txt has been deleted Program 2: This program uses unlink() function to delete a file from folder after using some operation. php <?php // PHP program to delete a file named gfg.txt // using unlink() function $file_pointer = fopen('gfg.txt', 'w+'); // writing on a file named gfg.txt fwrite($file_pointer, 'A computer science portal for geeks!'); fclose($file_pointer); // Use unlink() function to delete a file if (!unlink($file_pointer)) { echo ("$file_pointer cannot be deleted due to an error"); } else { echo ("$file_pointer has been deleted"); } ?> Output: Warning: unlink() expects parameter 1 to be a valid path, resource given in C:\xampp\htdocs\server.php on line 12 Resource id #3 cannot be deleted due to an error Note: If the file does not exist then it will display an error. PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Comment More infoAdvertise with us Next Article How to delete a file using PHP ? N NishanthReddy7 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads Deleting all files from a folder using PHP In PHP, files from a folder can be deleted using various approaches and inbuilt methods such as unlink, DirectoryIterator and DirectoryRecursiveIterator. Some of these approaches are explained below: Approach 1: Generate a list of files using glob() method Iterate over the list of files. Check wheth 3 min read How to get the file size using PHP ? In this article, we are going to discuss how to get the file size using PHP. PHP is a general-purpose scripting language that is suited for both server and client scripting language for website development. To get the file size, we will use filesize() function. The filesize() function returns the s 1 min read How to execute PHP code using command line? PHP Installation for Windows Users Follow the steps to install PHP on the Windows operating system. Step 1: First, we have to download PHP from its official website. We have to download the .zip file from the respective section depending upon our system architecture(x86 or x64).Step 2: Extract the . 2 min read How to copy a file from one directory to another using PHP ? The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bool 2 min read How to get the current file name using PHP script ? In this article, we will learn how to get the current filename using PHP. Input : c:/xampp/htdocs/project/home.php Output : project/home.php Input : c:/xampp/htdocs/project/abc.txt Output : project/abc.txt Sometimes, we need to get the current file name with the directory name in which our page is s 2 min read How to Convert File Content to Byte Array in PHP ? Converting file content to a byte array in PHP is a useful technique for various applications, including file manipulation, data processing, and when working with binary files like images or PDFs. PHP offers multiple ways to read file content and convert it into a byte array, providing flexibility t 5 min read How to move a file into a different folder on the server using PHP? The move_uploaded_file() function and rename() function is used to move a file into a different folder on the server. In this case, we have a file already uploaded in the temp directory of server from where the new directory is assigned by the method. The file temp is fully moved to a new location. 3 min read How to read data from a file stored in XAMPP webserver using PHP ? We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem. File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc fopen 2 min read How to recursively delete a directory and its entire contents (files + sub dirs) in PHP? In PHP if you want to delete the file or directory then keep one thing in mind that you cannot delete the file or directory directly there is a condition on that i.e. there are some security issues are there so the best way to do this is you first have to delete the data present in the file or the s 4 min read How to automatically start a download in PHP ? This post deals with creating a start downloading file using PHP. The idea is to make a download button which will redirect you to another page with the PHP script that will automatically start the download. Creating a download button: html <!DOCTYPE html> <html> <head> <meta na 2 min read Like