Home >Backend Development >PHP Problem >How to delete files in a specified directory in php

How to delete files in a specified directory in php

藏色散人
藏色散人Original
2020-07-27 09:27:274037browse

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.

How to delete files in a specified directory in php

Recommended: "PHP Tutorial"

Create a new php file, named test.php, for explanation How to delete a specified file in a directory using PHP.

How to delete files in a specified directory in 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:

How to delete files in a specified directory in php

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

How to delete files in a specified directory in php

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.

How to delete files in a specified directory in php

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.

How to delete files in a specified directory in php

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.

How to delete files in a specified directory in php

Run the test.php file in the browser to view the output results after executing the program.

How to delete files in a specified directory in php

How to delete files in a specified directory in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn