Matt123456 Matt123456 - 6 months ago 10
PHP Question

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????

Answer

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.

Comments