Deleting a file in PHP

Deleting a file in PHP

In PHP, you can delete a file using the unlink() function. The unlink() function takes a single parameter, which is the path to the file that you want to delete. Here’s an example of how to use the unlink() function to delete a file:

bash

Copy code

$file_path = “/path/to/file.txt”; if (file_exists($file_path)) { unlink($file_path); echo “File deleted successfully.”; } else { echo “File does not exist.”; }

In the code above, we first check if the file exists using the file_exists() function. If the file exists, we call the unlink() function to delete the file and print a message indicating that the file was deleted successfully. If the file does not exist, we print a message indicating that the file does not exist. It’s important to note that once a file is deleted using unlink(), it cannot be recovered.

Apply for PHP Certification!

https://www.vskills.in/certification/certified-php-developer

Back to Tutorials

Get industry recognized certification – Contact us

Menu