PHP/File Directory/copy

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

Copying a file

 
<?php
$old = "/tmp/yesterday.txt";
$new = "/tmp/today.txt";
copy($old,$new) or die("couldn"t copy $old to $new: $php_errormsg");
?>



Copying files

 
<?php 
  $source = "C:\\Apache\\logs\\error.log";
  $dest = "C:\\error.bak";
  if( copy( $source, $dest ) )
  {
    $msg = "Copied $source<br>to $dest";
  }
  else
  {
    $msg = "Unable to copy $source";
  }
?>
<html>
 <head>
  <title>Copying files</title>
 <head>
 <body>
  <?php echo($msg); ?>
 </body>
</html>



Copying Files with copy( )

 
<?
    $filename = "data.txt";
    $filename2 = $filename . ".old";
    $result = copy($filename, $filename2);
    if ($result) {
            print "$filename has been copied to $filename2.\n";
    } else {
            print "Error: couldn"t copy $filename to $filename2!\n";
    }
?>



Make a copy of a file: copy() function.

 
Its syntax is: int copy (string source, string destination)
<?
    $data_file = "data.txt";
    copy($data_file, $data_file."bak") or die("Could not copy $data_file");
?>



Using the copy() Function

 
<?php
     function move($source, $dest) {
          if(!copy($source, $dest)) return false;
          if(!unlink($source)) return false;
          return true;
     }
     if(!move("data.txt", "/tmp/tmpdir/newfile.txt")) {
                    echo "Error! Couldn"t move the file!<BR>";
     }
?>