PHP/File Directory/unlink

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

Delete a file with the unlink() function.

   <source lang="html4strict">

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

 </source>
   
  


Deleting a file

   <source lang="html4strict">

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

 </source>
   
  


Deleting files

   <source lang="html4strict">

<?php

 function delete( $file )
 {
   if( unlink( $file ) )
   { 
echo( "$file
has been deleted
" );
   }
   else
   { 
echo( "Unable to delete $file
" );
   }
 }

?> <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>

 </source>
   
  


Deleting Files with unlink( )

   <source lang="html4strict">

<?

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

?>

 </source>
   
  


Moving a file across filesystems

   <source lang="html4strict">

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

 unlink("/tmp/code.c");

} ?>

 </source>
   
  


Using file_exists, touch, and unlink together

   <source lang="html4strict">

<?php $file_name="test.txt"; if(file_exists($file_name)) {

   echo ("$file_name does exist.
");

} else {

   echo ("The file $file_name does not exist.
"); touch($file_name);

} if(file_exists($file_name)) {

   echo ("The file $file_name does exist.
"); unlink($file_name);

} else {

   echo ("The file $file_name does not exist.
");

} if(file_exists($file_name)) {

   echo ("The file $file_name does exist.
");

} else {

   echo ("The file $file_name does not exist.
");

} ?>

 </source>
   
  


Using the unlink() Function

   <source lang="html4strict">

<?php

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

?>

 </source>
   
  


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

   <source lang="html4strict">

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

 </source>