In PHP, you can use the unlink() function to delete a file. This function can delete the file with the specified path. The syntax is "unlink($filename,$context)"; the parameter "$filename" cannot be omitted. , used to specify the file path to be deleted, the parameter "$context" can be omitted, used to specify the environment of the file handle. The unlink() function returns TRUE if the file is successfully deleted, and FALSE if the file fails to be deleted.
The operating environment of this article: Windows 7 system, PHP8.1, Dell G3 computer.
In PHP, we can use the unlink() function to delete any file. The unlink() function accepts only one parameter: the file name. It is similar to UNIX C's unlink() function.
If the file has not been deleted, PHP unlink() will generate an E_WARNING level error. Returns TRUE if the file was successfully deleted, FALSE otherwise.
Syntax
bool unlink ( string $filename [, resource $context ] )
Among them, $filename is the file path to be deleted; $context is an optional parameter that specifies the environment of the file handle. $context is a set of options that modify the behavior of the stream.
PHP deletion file example
<?php $status=unlink('data.txt'); if($status){ echo "File deleted successfully"; }else{ echo "Sorry!"; } ?>
Execute the above code to get the following results
File deleted successfully
[Recommended: PHP video tutorial]
The above is the detailed content of How to delete a file in php. For more information, please follow other related articles on the PHP Chinese website!