Matt123456
Matt123456

Reputation: 99

PHP deleting files correctly

I have seen a comment about the unlink() function here

It says: The unlink() is not about removing file, it's about removing a file name. The manpage says: "unlink - delete a name and possibly the file it refers to."

I don't get it, will unlink() delete the files or will they be "invisible" and still waste space and performance????

Upvotes: 0

Views: 139

Answers (2)

Darren
Darren

Reputation: 13128

TL/DR

It basically means the space reserved for the said file is freed up for use within the system. All links to that file are removed, making the count 0. And 0 means no file, effectively freeing up the space.


unlink() will remove the link (the name of the file you specify) to the data which is coherently stored as (and referred to as) an inode.

When there are no more "links" to the data, the system automatically frees the associated space with that file. The number of links referenced to an inode are tracked right into the inode.

You can see how many links reference to a file by running the command ls -l

drwxr-xr-x    7 user      Administ     4096 Nov 25 07:46 app
drwxr-xr-x    5 user      Administ        0 Nov 24 14:25 inc

Where 7 and 5 are the respective links to the said file. Now when you run unlink() and remove the record from the directory (making the link count 0) allows the system to free up the related space.

Why you ask? Well when the links are 0 it means there are no links to the said data, effectively meaning there is no more link to the data, so it can be freed. Allowing you to use that freed up space.

This is exactly how hard linking and snapshots work.

Upvotes: 1

xtrman
xtrman

Reputation: 421

please refer: http://www.w3schools.com/php/func_filesystem_unlink.asp

All content has been taken from that page.

The unlink() function deletes a file.

example

<?php
$file = "test.txt";
if (!unlink($file)) {
    echo ("Error deleting $file");
} else {
    echo ("Deleted $file");
}
?>

Upvotes: 0

Related Questions