unlink

(PHP 3, PHP 4, PHP 5)

unlink -- Удаляет файл

Описание

bool unlink ( string filename [, resource context] )

Удаляет файл filename. Функция похожа на функцию unlink() Unix C. Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Замечание: Начиная с версии PHP 5.0.0, функция unlink() также может быть использована с некоторыми обвёртками URL. Обратитесь к Прил. M для получения списка обвёрток, которые поддерживают unlink().

Замечание: Аргумент context был добавлен начиная с версии PHP 5.0.0.

См. также описание функции rmdir() для удаления директорий.



unlink
khalidz83 at yahoo dot co dot uk
15-Feb-2006 07:18
If you get an error while deleting a file from the system, check who is the owner of the file. For example if you move to another system and the owner is 'nobody' then the owner should be nobody for that file on the new system.If you delete as another user, you will get an error.
Anarcho at ssm-clan dot de
26-Nov-2005 02:56
Just to be a bit more accurate to the post of "jchase at solidmark dot com":

On unixoide systems you only need write permission for the directory to delete a file. The permissions of the file are nonrelevant.

You need file permissions if you want to change the file data or to read the data.
bishop
05-Jun-2005 12:30
<?php
/**
 * rm() -- Vigorously erase files and directories.
 *
 * @param $fileglob mixed If string, must be a file name (foo.txt), glob pattern (*.txt), or directory name.
 *                        If array, must be an array of file names, glob patterns, or directories.
 */
function rm($fileglob)
{
   if (
is_string($fileglob)) {
       if (
is_file($fileglob)) {
           return
unlink($fileglob);
       } else if (
is_dir($fileglob)) {
          
$ok = rm("$fileglob/*");
           if (!
$ok) {
               return
false;
           }
           return
rmdir($fileglob);
       } else {
          
$matching = glob($fileglob);
           if (
$matching === false) {
              
trigger_error(sprintf('No files match supplied glob %s', $fileglob), E_USER_WARNING);
               return
false;
           }     
          
$rcs = array_map('rm', $matching);
           if (
in_array(false, $rcs)) {
               return
false;
           }
       }     
   } else if (
is_array($fileglob)) {
      
$rcs = array_map('rm', $fileglob);
       if (
in_array(false, $rcs)) {
           return
false;
       }
   } else {
      
trigger_error('Param #1 must be filename or glob pattern, or array of filenames or glob patterns', E_USER_ERROR);
       return
false;
   }

   return
true;
}
?>
jchase at solidmark dot com
20-May-2005 10:05
[Editor's note: A suggestion for a work-around was submitted by argistof at gmail dot com: You can use the recursive option (see man chmod) when chmodding, for instance 'chmod 777 directory/ -R'. Be aware though, this will change the permissions of all files and folders in the diectory.]

Just a note which you probably all know, but I didn't, and it might save another poor sap some unnecessary time:

I was doing unlink() and fopen() on a file and got a permission denied error, even after chmoding the file to 0777. 

The folder that contains the file must ALSO have write permission.  Took a headache to find this out. 

Hope this helps someone :)
kickthedonkey at gmail dot com
17-May-2005 04: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 02: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
06-Feb-2005 10: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.
chris at vibenewmedia dot com
14-Sep-2004 10: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 08:22
To delete files using wildcards:

<?
function delfile($str)
{
   foreach(
glob($str) as $fn) {
      
unlink($fn);
   }
}
?>

<umaskFirebird/InterBase>
 Last updated: Mon, 14 Nov 2005