Home  >  Article  >  Backend Development  >  How to delete local files using php

How to delete local files using php

藏色散人
藏色散人Original
2021-10-29 11:11:2410112browse

How to delete local files with PHP: 1. Create a new PHP file and set the encoding to utf8; 2. Create two variables, which are used to save the directory name of the deleted file and the name of the file to be deleted; 3. Use the file_exists() method to determine whether the directory exists; 4. Use the unlink() method to delete the file.

How to delete local files using php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to delete local files using php?

Use PHP to delete a specified file in a directory:

Create a new php file named test.php to explain how to delete it using PHP A file specified in a directory.

How to delete local files 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:

How to delete local files using 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 local files using 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 local files using 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 local files using 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 local files using php

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

How to delete local files using php

How to delete local files using php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete local files using 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