Home  >  Article  >  Backend Development  >  How to delete a file in php

How to delete a file in php

藏色散人
藏色散人Original
2021-02-24 09:22:427454browse

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.

How to delete a file in php

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(&#39;data.txt&#39;);    
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!

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