How to delete a file in php

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

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

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

where $filename is the file name to be deleted, and $context is the optional context parameter.

The following is a simple example demonstrating how to use the unlink() 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 use file_exists( ) Function checks whether the file exists. If the file exists, use the unlink() 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 the unlink() function, there are other PHP functions that can be used to delete files, such as unlink(), rmdir() and unlink(). Typically, however, the unlink() function is the most commonly used.

In short, a file can be easily deleted using the unlink() 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template