PHP/File Directory/unlink — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:04, 26 мая 2010

Delete a file with the unlink() function.

 
Its syntax is: int unlink (string file)
<?
unlink("data.txt");
?>



Deleting a file

 
<?php
$file = "/tmp/junk.txt";
unlink($file) or die ("can"t delete $file: $php_errormsg");
?>



Deleting files

 
<?php   
  function delete( $file )
  {
    if( unlink( $file ) )
    { 
      echo( "$file<br>has been deleted<hr>" ); 
    }
    else
    { 
      echo( "Unable to delete $file<hr>" ); 
    }
  }
?>
<html>
 <head>
  <title>Deleting files</title>
 <head>
 <body>
 <?php 
  # for Linux use...
  #$file_A = "/errlog_bak";
  #$file_B = "/errlog_not";
  # for Windows use...
  $file_A = "C:\\errlog.bak";
  $file_B = "C:\\errlog.not";
  delete($file_A);
  @delete($file_B);
  ?>
 </body>
</html>



Deleting Files with unlink( )

 
<?
    $filename = "data.txt";
    if (unlink($filename)) {
            print "Deleted $filename!\n";
    } else {
            print "Delete of $filename failed!\n";
    }
?>



Moving a file across filesystems

 
<?php
if (copy("/tmp/code.c","/usr/local/src/code.c")) {
  unlink("/tmp/code.c");
}
?>



Using file_exists, touch, and unlink together

 
<?php
$file_name="test.txt";
if(file_exists($file_name)) {
    echo ("$file_name does exist.<br />");
}
else {
    echo ("The file $file_name does not exist.<br />");
    touch($file_name);
}
if(file_exists($file_name)) {
    echo ("The file $file_name does exist.<br />");
    unlink($file_name);
}
else {
    echo ("The file $file_name does not exist.<br />");
}
if(file_exists($file_name)) {
    echo ("The file $file_name does exist.<br />");
}
else {
    echo ("The file $file_name does not exist.<br />");
}
?>



Using the unlink() Function

 
<?php
     $files = glob("/tmp/*.tmp");
     foreach($files as $val) {
               unlink($val);
     }
?>



You can remove an existing file with the unlink() function.

 
<?
unlink("myfile.txt");
?>