In php, we can use the unlink function to delete a specified file. The following describes how to delete a specified file by the unlink function, for more information, see.
The unlink () function deletes a file.
If the call succeeds, true is returned. If the call fails, false is returned.
The Code is as follows: |
Copy code |
<? Php Unlink ("test.txt "); ?> |
Example
The Code is as follows: |
Copy code |
<? Php $ File = "test.txt"; if (! Unlink ($ file )){ Echo ("Error deleting $ file");} else { Echo ("Deleted $ file ");} ?> |
Add judgment
The Code is as follows: |
Copy code |
<? Php $ File = "test.txt "; If (unlink ($ file )) { Echo "file $ file deleted successfully! "; } Else { Echo "file $ file deletion failed! "; } ?> </Body> </Html> |
Output result:
The file test.txt is deleted successfully!
The following code checks whether a file exists:
The Code is as follows: |
Copy code |
<? Php $ Myfile = "./test1.txt "; If (file_exists ($ myfile )){ $ Result = unlink ($ myfile ); Echo $ result; } ?> |
The rmdir () function deletes a directory and returns TRUE if the directory is successfully deleted. Otherwise, FALSE is returned.
Syntax:
Bool rmdir (string dirname)
Example:
The Code is as follows: |
Copy code |
<? Php $ Dirname = 'testdir '; If (rmdir ($ dirname )){ Echo "directory $ dirname deleted successfully "; } Else { Echo "directory $ dirname deletion failed "; } ?> |