How to delete a file in php

PHPz
Release: 2023-03-29 13:36:35
Original
528 people have browsed it

In PHP, we can use theunlink()function to delete files. The syntax of this function is as follows:

bool unlink ( string $filename [, resource $context ] )
Copy after login

where$filenameis the file name to be deleted, and$contextis the optional context parameter.

The following is a simple example demonstrating how to use theunlink()function to delete a file:

$file = '/path/to/file.txt'; if (file_exists($file)) { if (unlink($file)) { echo '文件已成功删除。'; } else { echo '文件删除失败。'; } } else { echo '文件不存在。'; }
Copy after login

In the above example, first usefile_exists( )Function checks whether the file exists. If the file exists, use theunlink()function to delete it. If the deletion is successful, "The file was successfully deleted." will be output; if the deletion fails, "File deletion failed." will be output; if the file does not exist, "The file does not exist." will be output.

In addition to theunlink()function, there are other PHP functions that can be used to delete files, such asunlink(),rmdir()andunlink(). Typically, however, theunlink()function is the most commonly used.

In short, a file can be easily deleted using theunlink()function. When you need to delete a file, just call this function and provide the name of the file you want to delete.

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!