How can you delete file from php?

  • Last Updated : 06/08/2021

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.
So, In this post, we will learn how to delete a file from a server if the file exists using the PHP unlink function.

How can you delete file from php?

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";
}
$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:

<?php
// PHP program for deleting all
// file from a folder
   
// Folder path to be flushed
$folder_path = "myw3docs";
   
// List of name of files inside
// specified folder
$files = glob($folder_path.'/*'); 
   
// Delete all the files of the list
foreach($files as $file) {
   
    if(is_file($file)) 
    
        // Deleting the given file
        unlink($file); 
}
?>

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:

<?php
// PHP program for deleting all files from a folder
  
// Delete all the files inside the given folder
array_map('unlink', array_filter(glob("myw3Docs/*"), 'is_file'));  
?>

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:

<?php
// PHP program for deleting all the files
// from a folder
  
// Folder path to be flushed
$folder_path = 'myw3docs/';
  
// Assign files inside the directory
$dir = new DirectoryIterator(dirname($folder_path));
  
// Delete all the files in the list
foreach ($dir as $fileinfo) {
      
    if (!$fileinfo->isDot() && $fileinfo->isFile()) {
  
        // Deleting the given file
        unlink($fileinfo->getPathname());
    }
}
?>

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 function

To delete a file, you use the unlink() function:

unlink ( string $filename , resource $context = ? ) : bool

Code language: PHP (php)

The unlink() function has two parameters:

  • $filename is the full path to the file that you want to delete.
  • $context is a valid context resource.

The unlink() function returns true if it deletes the file successfully or false otherwise. If the $filename doesn’t exist, the unlink() function also issues a warning and returns false.

Let’s take some examples of using the unlink() function.

1) Simple PHP delete file example

The following example uses the unlink() function to delete the readme.txt file:

<?php $filename = 'readme.txt'; if (unlink($filename)) { echo 'The file ' . $filename . ' was deleted successfully!'; } else { echo 'There was a error deleting the file ' . $filename; }

Code language: HTML, XML (xml)

2) Delete all files in a directory that match a pattern

The following example deletes all files with the .tmp extension:

<?php $dir = 'temp/'; array_map('unlink', glob("{$dir}*.tmp"));

Code language: HTML, XML (xml)

How it works.

  • First, define a variable that stores the path to the directory in which you want to delete files.
  • Second, use the glob() function to search for all files in the directory $dir that has the *.tmp extension and pass it result to the array_map() function to delete the files.

Generally, you can change the pattern to delete all matching files in a directory using the array_map(), unlink() and glob() functions.

Summary

  • Use PHP unlink() function to delete a file.

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..