Master PHP’s unlink(): Delete Files & Directories Safely

Learn how to use PHP’s unlink() function to delete files and empty directories, understand its syntax, handle return values, see practical code examples for file and directory removal, and discover important considerations such as handling non‑empty directories and error checking.

php Courses
php Courses
php Courses
Master PHP’s unlink(): Delete Files & Directories Safely

In PHP development, we often need to delete files or directories. The PHP function unlink() is a commonly used function for deleting files. This article details its usage to help readers understand and apply it.

unlink() Function Basic Syntax

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

Here, $filename is the path of the file to delete, and $context is an optional parameter representing the context to apply when opening the file.

The unlink() function returns a boolean value: true on successful deletion, false on failure.

Using unlink()

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

<?php
$filename = 'test.txt';

if (unlink($filename)) {
    echo "File deleted successfully!";
} else {
    echo "File deletion failed!";
}
?>

In the code above, we define the variable $filename as the path of the file to delete, then call unlink(). If the deletion succeeds, it outputs “File deleted successfully!”, otherwise it outputs “File deletion failed!”.

Besides deleting files, unlink() can also delete directories. Below is an example of deleting a directory:

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

Here, $dirname represents the directory path. We first check whether the directory exists with is_dir(). If it exists, we attempt to delete it with unlink(). Successful deletion outputs “Directory deleted successfully!”, otherwise it outputs “Directory deletion failed!”. If the directory does not exist, it outputs “Directory does not exist!”.

Note that unlink() can only delete files or empty directories. To delete a non‑empty directory, you must first remove its contents using rmdir() (or other file‑deletion methods) and then call unlink() to remove the directory itself.

Summary

This article thoroughly explains the usage of PHP’s unlink() function. By using unlink(), you can conveniently delete files and empty directories. When using the function, pay attention to parameter passing and error handling to ensure the deletion operation proceeds smoothly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPfile deletionphp-functionsdirectory 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

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.