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

unlink

(PHP 3, PHP 4 )

unlink -- Deletes a file

Description

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
unlink
pablorATnkstudiosDOTnet - pi.nks.com.ar
16-Oct-2003 12:54
Hi!

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 php.net manual comments)
 * @Author: Pablo Rosciani [ pabloATnkstudiosDOTnet ] [ http://pi.nks.com.ar ]
 * @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;
            }
        }
       
closedir($dh);
       
       
       
//Split file name and extenssion
       
if(strpos($pattern,".")) {
           
$baseexp=substr($pattern,0,strpos($pattern,"."));
           
$typeexp=substr($pattern,strpos($pattern,".")+1,strlen($pattern));
        }else{ 
           
$baseexp=$pattern;
           
$typeexp="";
        } 
       
       
//Escape all regexp Characters
       
$baseexp=preg_quote($baseexp); 
       
$typeexp=preg_quote($typeexp); 
       
       
// Allow ? and *
       
$baseexp=str_replace(array("\*","\?"), array(".*","."), $baseexp);
       
$typeexp=str_replace(array("\*","\?"), array(".*","."), $typeexp);
       
       
//Search for pattern match
       
$i=0;
        foreach(
$files as $file) {
           
$filename=basename($file);
            if(
strpos($filename,".")) {
               
$base=substr($filename,0,strpos($filename,"."));
               
$type=substr($filename,strpos($filename,".")+1,strlen($filename));
            }else{
               
$base=$filename;
               
$type="";
            }
       
            if(
preg_match("/^".$baseexp."$/i",$base) && preg_match("/^".$typeexp."$/i",$type))  {
               
$matches[$i]=$file;
               
$i++;
            }
        }
       
        while(list(
$idx,$val) = each($matches)){
            if (
substr($dir,-1) == "/"){
               
unlink($dir.$val);
            }else{
               
unlink($dir."/".$val);
            }
        }
       
    }
}

unlink_wc("/path/to/folder/","*.*");
?>
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/foo.bar');

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;

config.php:
<?
define
('SERVER_PATH_TO_WEB_ROOT','/home/httpdoc/foo',FALSE);
?>

deleter.php:
<?
include('config.php');

unlink(SERVER_PATH_TO_WEB_ROOT."/myfile.bar");
?>

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);
   clearstatcache();
   if (@file_exists($file)) {
      $filesys = eregi_replace("/","\\",$file);
      $delete = @system("del $filesys");
      clearstatcache();
      if (@file_exists($file)) {
         $delete = @chmod ($file, 0775);
         $delete = @unlink($file);
         $delete = @system("del $filesys");
      }
   }
   clearstatcache();
   if (@file_exists($file)){
      return false;
      }
      else{
            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.

thisfile.php:

<?
    unlink
("thisfile.php");
?>
 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.

<umaskFDF>
 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: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST