php code to delete files is "unlink($file,$cont)". The unlink() function is used to delete the specified file. It accepts a non-omit parameter "$file", which specifies the file path to be deleted, and an omitted parameter "$cont", which specifies the file handle environment and can modify the behavior of the stream. A set of options.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, if you want to delete files, you can Use the unlink() function.
The unlink() function can delete the specified file. It returns TRUE when the function is executed successfully and FALSE when it fails.
The code to delete the file is as follows:
unlink($file,$cont)
Parameter | Description |
---|---|
$file | Required. Specifies the path of the file to be deleted. |
$cont | Optional. Specifies the environment for a file handle. cont is a set of options that can modify the behavior of the stream. |
Example: Delete the specified file newtest.txt
<?php header("content-type:text/html;charset=utf-8"); $file = 'newtest.txt'; if(file_exists($file)){ if(unlink($file)){ echo $file.' 删除成功!'; }else{ echo $file.' 删除失败!'; } }else{ echo $file.' 不存在!'; } ?>
at Refresh it and it will display:
Because the file has been deleted, it no longer exists.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the code to delete files in php. For more information, please follow other related articles on the PHP Chinese website!