How to delete a file in php

藏色散人
Release: 2023-03-08 08:40:01
Original
7517 people have browsed it

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 ] )
Copy after login

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!";    
}  
?>
Copy after login

Execute the above code to get the following results

File deleted successfully
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template