What is the php command for deleting a file?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink[] function that allows to delete a file. The PHP unlink[] function takes two parameters $filename and $context. Syntax:

    unlink[ $filename, $context ];

    Below programs illustrate the above approach: Program 1: This program uses unlink[] function to remove file from directory. Suppose there is a file named as “gfg.txt” 

    php

    Output:

    gfg.txt has been deleted

    Program 2: This program uses unlink[] function to delete a file from folder after using some operation. 

    php

    Output:

    Warning: unlink[] expects parameter 1 to be a valid path, resource
    given in C:\xampp\htdocs\server.php on line 12
    Resource id #3 cannot be deleted due to an error

    Note: If the file does not exist then it will display an error.

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    Change language:

    Submit a Pull Request Report a Bug

    deleteSee unlink[] or unset[]

    Description

    There is no delete keyword or function in the PHP language. If you arrived at this page seeking to delete a file, try unlink[]. To delete a variable from the local scope, check out unset[].

    See Also

    • unlink[] - Deletes a file
    • unset[] - Unset a given variable

    +add a note

    User Contributed Notes

    There are no user contributed notes for this page.

    [PHP 4, PHP 5, PHP 7, PHP 8]

    unlinkDeletes a file

    Description

    unlink[string $filename, ?resource $context = null]: bool

    Parameters

    filename

    Path to the file.

    If the file is a symlink, the symlink will be deleted. On Windows, to delete a symlink to a directory, rmdir[] has to be used instead.

    context

    A context stream resource.

    Return Values

    Returns true on success or false on failure.

    Changelog

    VersionDescription
    7.3.0 On Windows, it is now possible to unlink[] files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.

    Examples

    Example #1 Basic unlink[] usage

    See Also

    • rmdir[] - Removes directory

    anagai at yahoo dot com

    10 years ago

    This will delete all files in a directory matching a pattern in one line of code.

    dexen dot devries at gmail dot com

    11 years ago

    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.

    The caveat:
    A file's body may *also* be `kept alive' [still using diskspace] by a process holding the file open. The body will not be deallocated [will not free disk space] as long as the process holds it open. In fact, there's a fancy way of resurrecting a file removed by a mistake but still held open by a process...

    deen804 at gmail dot com

    8 years ago

    unlink[$fileName]; failed for me .
    Then i tried using the realpath[$fileName]  function as
    unlink[realpath[$fileName]]; it worked

    just posting it , in case if any one finds it useful .

    federico at poisonfx dot com

    11 years ago

    Here the simplest way to delete files with mask

    chris at vibenewmedia dot com

    18 years ago

    To delete all files of a particular extension, or infact, delete all with wildcard, a much simplar way is to use the glob function.  Say I wanted to delete all jpgs .........

    PD

    14 years ago

    I have been working on some little tryout where a backup file was created before modifying the main textfile. Then when an error is thrown, the main file will be deleted [unlinked] and the backup file is returned instead.

    Though, I have been breaking my head for about an hour on why I couldn't get my persmissions right to unlink the main file.

    Finally I knew what was wrong: because I was working on the file and hadn't yet closed the file, it was still in use and ofcourse couldn't be deleted :]

    So I thought of mentoining this here, to avoid others of making the same mistake:

    Chủ Đề