Backend Development 5 min read

How to Use PHP unlink() to Delete Files and Directories

This tutorial explains the PHP unlink() function, covering its syntax, parameters, return values, and practical examples for deleting both files and empty directories, while also noting its limitations and the need for rmdir() when handling non‑empty directories.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP unlink() to Delete Files and Directories

In PHP development we often need to delete files or directories, and the unlink() function is a commonly used way to delete files. This article provides a detailed introduction to the usage of unlink() to help readers understand and apply the function effectively.

The basic usage of unlink() is simple: it accepts a single argument, the path of the file to be deleted.

unlink() Function Basic Syntax

<code>bool unlink ( string $filename [, resource $context ] )</code>

Here, $filename represents the path of the file to delete, and $context is an optional parameter that specifies the context to be used when opening the file.

The function returns a boolean value: true on successful deletion and false on failure.

unlink() Function Usage

First, create a text file named test.txt , then use unlink() to delete this file:

<code>&lt;?php
$filename = 'test.txt';
if (unlink($filename)) {
    echo "File deleted successfully!";
} else {
    echo "File deletion failed!";
}
?&gt;</code>

In the code above, the variable $filename holds the path of the file to be removed, and unlink() attempts to delete it, outputting a success or failure message accordingly.

Besides deleting files, unlink() can also delete directories. The following example demonstrates how to delete a directory:

<code>&lt;?php
$dirname = 'test';
if (is_dir($dirname)) {
    if (unlink($dirname)) {
        echo "Directory deleted successfully!";
    } else {
        echo "Directory deletion failed!";
    }
} else {
    echo "Directory does not exist!";
}
?&gt;</code>

This code first checks whether $dirname is a directory using is_dir() . If it exists, unlink() attempts to delete it, reporting the outcome; otherwise it reports that the directory does not exist.

It is important to note that unlink() can only delete files or empty directories. To remove a non‑empty directory, you must first delete its contents with rmdir() (or other file‑deletion functions) and then use unlink() to remove the directory itself.

Summary

This article has thoroughly introduced the PHP unlink() function. By using unlink() , developers can conveniently delete files or empty directories. When using the function, pay attention to parameter passing and error handling to ensure successful deletion operations.

PHP Learning Recommendations

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Advanced Recommended Courses

Workerman+TP6 Real‑time Chat System Limited‑time Offer

Backend Developmentphpfile deletiondirectory deletionunlink
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.