search
HomeBackend DevelopmentPHP ProblemHow to delete local files using php

How to delete local files with PHP: 1. Create a new PHP file and set the encoding to utf8; 2. Create two variables, which are used to save the directory name of the deleted file and the name of the file to be deleted; 3. Use the file_exists() method to determine whether the directory exists; 4. Use the unlink() method to delete the file.

How to delete local files using php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to delete local files using php?

Use PHP to delete a specified file in a directory:

Create a new php file named test.php to explain how to delete it using PHP A file specified in a directory.

How to delete local files using php

In the test.php file, use the header() method to set the encoding of the file execution to utf8 to avoid garbled characters when the page outputs Chinese. The code is as follows:

How to delete local files using php

#In the test.php file, create two variables, one is the $path variable, which is used to save the directory name of the deleted file; the other is $filename Variable used to save the file name to be deleted.

How to delete local files using php

In the test.php file, use the file_exists() method to determine whether the directory exists. If the directory exists, execute the program statement in the if.

How to delete local files using php

In the test.php file, connect the $path directory and $filename file name to the path to delete the file, and use the unlink() method to delete it. The file specified by this path.

How to delete local files using php

In the test.php file, use the if statement to determine the result of unlink() deletion. If the return value is true, then output "File deletion successful!", if If false is returned, "Failed to delete file!" is output.

How to delete local files using php

Run the test.php file in the browser to view the output results after executing the program.

How to delete local files using php

How to delete local files using php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete local files using 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment