Home  >  Article  >  Backend Development  >  How to delete files under a specified path in php

How to delete files under a specified path in php

王林
王林Original
2020-09-29 11:14:092974browse

php method to delete files under the specified path: first use the file_exists() function to determine whether the specified path exists; then use the unlink() function to delete the files under the specified path.

How to delete files under a specified path in php

Solution:

(Recommended tutorial: php video tutorial)

  • Use the header() method to set the encoding of file execution to utf8 to avoid garbled characters when the page outputs Chinese.

  • Create two variables, one is the $path variable for saving The directory name of the deleted file; the other is the $filename variable, which is used to save the name of the file to be deleted.

  • Use the file_exists() method to determine whether the directory exists. If the directory exists, execute the program statement within the if.

  • Connect the $path directory and $filename file name into the path to delete the file, and use the unlink() method to delete the file specified by this path.

  • Use the if statement to determine the result of deletion by unlink(). If it returns true, it will output "The file was deleted successfully!". If it returns false, it will output "Failed to delete the file." !".

Code implementation:

How to delete files under a specified path in php

Related recommendations: php training

The above is the detailed content of How to delete files under a specified path 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