What is the difference between delete() and unlink() in PHP
Asked Answered
M

7

18

When I started searching for "How to delete a file in PHP" The solution I got almost everywhere is "unlink()"

But in w3schools I met with another function named delete(). Here is that link delete() function w3schools

And I started surfing about delete() but didn't get much answers..

This is the question similar to my question at stackoverflow.. DIfferent between unlink() and delete() on unix

I really would like to know the difference and similarities between these two functions.. Why we are using unlink() instead of delete().

Mosira answered 26/8, 2014 at 5:44 Comment(2)
Do read the manual php.net/manual/en/function.delete.php - "This is a dummy manual entry to satisfy those people who are looking for unlink() or unset() in the wrong place."Stoeber
Don't use W3Schools.Tympan
O
16

delete() function doesn't exist as the php docs says

This is a dummy manual entry to satisfy those people who are looking for unlink() or unset() in the wrong place.

a dummy manual entry was created to catch anyone that is looking for a function that they assume to exist but doesn't really exist then guide them to the right function

http://php.net/manual/en/function.delete.php

Outgo answered 26/8, 2014 at 5:50 Comment(0)
S
5

php is a C like language - the syntax is similar, most of the functions in PHP are pulled from C standard libraries (and POSIX)

"unlink" is a C function. (originally POSIX, but recently standardised by ISO as _unlink)

"delete" is a CP/M command which is same as the "era" CP/M command

Unlink refers to the underlying UNIX command, unlink, which removes the symbolic or hard link to the file, not necessarily the file itself. The file is only removed when all links to the file are removed.

Strand answered 26/8, 2014 at 5:53 Comment(0)
P
3

According to php.net delete is not function in php .Use unlink function to delete file. http://php.net/manual/en/function.delete.php

Petrarch answered 26/8, 2014 at 6:5 Comment(0)
S
2

in Php, delete() is dummy manual entry to satisfy those people who are looking for unlink() or unset() in the wrong place.

A "dummy entry" is a "fake" entry or, in this case, an entry for a command that does not actually exist, so that people can find the correct one.

See more details here: php.net-manual-function.delete

Schram answered 26/8, 2014 at 5:50 Comment(0)
P
1

There is no delete function in php

reference http://php.net/manual/en/function.delete.php

we have to use unlink to delete an file

Pinchcock answered 26/8, 2014 at 5:50 Comment(0)
C
1

Reference :

Deleted a large file but seeing no increase in free space or decrease of disk usage? Using UNIX or other POSIX OS?

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''.

Most of the time a file has just one name -- removing it will also remove (free, deallocate) the `body' of file (with one caveat, see below). That's the simple, usual case.

However, it's perfectly fine for a file to have several names (see the link() function), in the same or different directories. All the names will refer to the file body and `keep it alive', so to say. Only when all the names are removed, the body of file actually is freed.

Calvinism answered 26/8, 2014 at 5:54 Comment(1)
There are also links to a file from the memory system, not just from the file system. When a file is opened by a running process, an extra link is established pointing to that file. Even if all of the file system links are deleted, so long as a running process has that file open the file remains intact on the disk. This prevents accidental deletion of a file in use by a program. It also means that a zombie process can keep a chunk of filespace tied up even when its files have been "deleted." The process must also be killed in order to return the blocks to the free space list.Clave
B
1

Delete is not a core PHP function , while unlink is a core php function.

<?php


print "Below examples clear the diff between unlink and delete";

/* There is file abc.txt in foo DIR */

/*
* Operation 1: Use delete function
* Operation 2: Use unlink function
*
*/

$path  = $_SERVER['DOCUMENT_ROOT'] ."/foo/abc.txt";

## Uncomment to print the path
//print "Path = ". $path;

## Uncomment to see what delete throws an output
//delete($path) or die("I have some problem here");

/* output: Here it will show ... delete is not a function */

unlink($path) or die("File is not deleted. Specify some valid path till file.");
Beffrey answered 26/8, 2014 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.