PHP: Delete or Unlink a File on the Server

PHP has the capacity to work with server-side files in many ways creating, opening, reading, and writing. When you are done with a file, it is a simple matter to delete it using a PHP script.


When a file is no longer needed on the server, as with temporary files, it can be deleted using the unlink() function. To delete a file referenced by the variable $tmpfile, use the following code:

$tmpfile = "/tmp/somefile.txt";
unlink($tmpfile);

The unlink() function returns true if the file was successfully deleted and false if a problem has occurred.

If there is a problem, it is most likely because you do not have permission to delete the file. In the case of UNIX filesystems, unlinking a file requires write access to the directory containing the file, not to the file itself. If PHP created the file initially, then this is not an issue since the directory must be writable to create a file.

If this does not seem reasonable, think about a directory as a file which contains the information about the contents of the directory. To delete a file, no changes are required to the file, but the directory containing the file must be altered to reflect the deletion. Therefore, only the directory must be writable by the user in order to remove the file.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

11 Responses to “PHP: Delete or Unlink a File on the Server”

  1. January 12, 2009 at 12:51 am, Anonymous said:

    a nice n short article on how to delete a file using PHP codes. Good work. Thank you for sharing.

    Reply

  2. April 20, 2009 at 2:36 pm, Christopher said:

    Sometimes it’s a good idea to check if the file exists first, then you won’t get a PHP error if the file you’re trying to delete is not there. Just add a simple IF statement to accomplish this:

    $tmpfile = “/full/path/to/delete-me.txt”;
    if (file_exists($tmpfile)) { unlink ($tmpfile); }

    Reply

  3. May 22, 2009 at 6:24 am, Nitish said:

    hmmm…. good post…

    Reply

  4. February 16, 2010 at 2:08 pm, Souvik Banerjee said:

    What kind of advertisement is this… cant read the article… please remove it. or fix it.

    Reply

  5. April 21, 2010 at 5:47 am, Anonymous said:

    Excellent tip! That solved my problem in four files, accidentally created on my webhost, these files were not deleted in any FTP client (windows, linux, etc) because you have “” caracter in the file name and received the message 550 prohibited filename. With this tip, the files had been removed!

    Reply

    • March 18, 2011 at 9:40 am, Dddd said:

      kus ke bachchey.. teri bhen ko chodu..

      Reply

  6. May 28, 2010 at 3:11 am, good said:

    erwtewtewrt
    wert

    Reply

  7. November 05, 2010 at 7:41 am, Tshepo said:

    Hi every one, I would like to know, how can I delete a file from a server? My problem actually lies on the path that I must specify so that when I issue the unlink() function it is able to find the specific file I’m referring to and delete it right away.

    Reply

  8. December 13, 2010 at 8:14 am, Kashif Webpro1 said:

    Simple & easy

    Reply

  9. March 23, 2011 at 9:46 am, Homesh said:

    well i have fix my problem…..

    Reply

  10. August 12, 2011 at 7:08 am, Robert said:

    simple and to the point, would be good to maybe expand this to show you how to delete an entry in a sql database? i.e the path to the file as well as the file itself

    Reply

Leave a Reply