downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003


(PHP 3, PHP 4 )

unlink -- Deletes a file


bool unlink ( string filename)

Deletes filename. Similar to the Unix C unlink() function. Returns TRUE on success or FALSE on failure.

Note: As of PHP 5.0.0 unlink() can also be used with some url wrappers. Refer to Appendix I for a listing of which wrappers support unlink().

See also rmdir() for removing directories.

add a note add a note User Contributed Notes
pablorATnkstudiosDOTnet -
16-Oct-2003 12:54

I made a unlink() function but with esteroids, now you can delete files using wildcards like * and/or ?

 * @Name: unlink_wc($dir,$pattern)
 * @Description: A unlink function that support wildcards ( * and ? )
 * (based on code taked from manual comments)
 * @Author: Pablo Rosciani [ pabloATnkstudiosDOTnet ] [ ]
 * @Date: 15/10/03
 * @param string $dir       Directory to search files
 * @param string $pattern   Pattern to find.

function unlink_wc($dir$pattern){
    if (
$dh opendir($dir)) { 
//List and put into an array all files
while (false !== ($file readdir($dh))){
            if (
$file != "." && $file != "..") {
$files[] = $file;
//Split file name and extenssion
if(strpos($pattern,".")) {
//Escape all regexp Characters
// Allow ? and *
$baseexp=str_replace(array("\*","\?"), array(".*","."), $baseexp);
$typeexp=str_replace(array("\*","\?"), array(".*","."), $typeexp);
//Search for pattern match
$files as $file) {
strpos($filename,".")) {
preg_match("/^".$baseexp."$/i",$base) && preg_match("/^".$typeexp."$/i",$type))  {
$idx,$val) = each($matches)){
            if (
substr($dir,-1) == "/"){

jsvlrt @t yahoo (dotcom)
10-Jun-2003 01:02
in response to rich at pulse8design dot com;

also, if, for instance you were calling

system('rm /home/');

and the script was later moved to a windows server, this line would need to be changed as the rm command only exists on un*x, however, unlink would still work, providing the paths were the same, I use something like;




this way, I only have to change one setting if the server structure changes, and I don't need to worry about un*x / windows issues (as much)
richard at keyms dot com
10-Feb-2003 07:39
when you have opened the file with fopen() you need to use fclose() before you can unlink them :)
bjorn AT smokingmusic DOT com
25-Nov-2002 10:21
in follow up to    icecube at fr dot fm     
and the comment about clearstatcache()...

.... so that would make the 'delete function' like this:

function delete_file($file){
   $delete = @unlink($file);
   if (@file_exists($file)) {
      $filesys = eregi_replace("/","\\",$file);
      $delete = @system("del $filesys");
      if (@file_exists($file)) {
         $delete = @chmod ($file, 0775);
         $delete = @unlink($file);
         $delete = @system("del $filesys");
   if (@file_exists($file)){
      return false;
            return true;
}  // end function
volker at duetsch dot net
18-Jul-2002 04:25
To delete all files in a specified path I use on my W2K server (apache 1.3.24 and php 4.1.1.) the following:<br>
$path = "c:\\htdocs\\apps\\qis\\pdf\\temp\\";
passthru ("del $path* /q");
quanghoc at netzero dot net
02-Feb-2002 01:50
You can run a program to delete the program file itself. There is no error.


 Run thisfile.php to delete itself. This purpose is to delete some important program file after one time run.
thomasj at superusers dot dk
06-Jul-2000 05:42
if you can't unlink a file it might be, that there is no write permission for the DIRECTORY in which the file resides.  Check this out.

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST