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