search for in the  
<umaskFriBiDi>
Last updated: Thu, 19 May 2005

unlink

(PHP 3, PHP 4, PHP 5)

unlink -- Deletes a file

Description

bool unlink ( string filename [, resource context] )

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 L for a listing of which wrappers support unlink().

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CXXII, Stream Functions.

See also rmdir() for removing directories.



User Contributed Notes
unlink
kickthedonkey at gmail dot com
17-May-2005 06:24
Regarding the comment by 'admin at crazydrumguy dot info':  You might want to consider using is_file instead of file_exists if you're wanting to target a file specifically (and not a directory).  file_exists($name) will return true if $name contains a path to a directory (which may or may not be what you want to do...)
ashley at semantic dot org
02-Apr-2005 04:50
Actually you should use "@unlink" rather than testing with file_exists. The former is atomic, whereas the latter can break if you can't guarantee only one process will try to delete a given file at a time.
dagski_AT_gmail_DOT_com
07-Feb-2005 12:19
before you could unlink a file created which uses a handle e.g.,

$handle = sqlite('temp.db');

unset($handle); first befofe
unlink($handle);

to avoide permission denied error.
admin at crazydrumguy dot info
22-Jan-2005 07:43
Regarding the previous note, you can accomplish the same thing by using file_exists.
<?php
if(file_exists($file))
unlink($file);
?>
xenon54 at generationphp dot net
28-Dec-2004 10:03
Use @ to avoid having a warning error if the file is not found.

<?php
$return
= @unlink('filename.txt');
// Return FALSE if not found
var_dump($return);
?>
chris at vibenewmedia dot com
14-Sep-2004 12:54
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 .........

<?php

foreach (glob("*.jpg") as $filename) {
   echo
"$filename size " . filesize($filename) . "\n";
  
unlink($filename);
}

?>
pc AT newagelab DOT com DOT ua
08-Sep-2004 10:22
To delete files using wildcards:

<?
function delfile($str)
{
   foreach(
glob($str) as $fn) {
      
unlink($fn);
   }
}
?>
richard at keyms dot com
10-Feb-2003 05:39
when you have opened the file with fopen() you need to use fclose() before you can unlink them :)

<umaskFriBiDi>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT