Home >Backend Development >PHP Problem >How to delete files in a specified directory in php
php method to delete files in a specified directory: first use the header method to set the encoding of file execution to utf8; then create two variables; then use the "file_exists" method to determine whether the directory exists; finally use the unlink method to delete the path The specified file is sufficient.
Recommended: "PHP Tutorial"
Create a new php file, named test.php, for explanation How to delete a specified file in a directory using PHP.
In the test.php file, use the header() method to set the encoding of the file execution to utf8 to avoid garbled characters when the page outputs Chinese. The code is as follows:
#In the test.php file, create two variables, one is the $path variable, which is used to save the directory name of the deleted file; the other is $filename Variable used to save the file name to be deleted.
In the test.php file, use the file_exists() method to determine whether the directory exists. If the directory exists, execute the program statement in the if.
In the test.php file, connect the $path directory and $filename file name to the path to delete the file, and use the unlink() method to delete it. The file specified by this path.
In the test.php file, use the if statement to determine the result of unlink() deletion. If the return value is true, then output "File deletion successful!", if If false is returned, "Failed to delete file!" is output.
Run the test.php file in the browser to view the output results after executing the program.
The above is the detailed content of How to delete files in a specified directory in php. For more information, please follow other related articles on the PHP Chinese website!