Приглашаем посетить
Цветаева (tsvetaeva.lit-info.ru)

Copying, Moving, and Deleting Files

Previous
Table of Contents
Next

Copying, Moving, and Deleting Files

copy($filename, $tempname1);
rename($tempname1, $tempname2);
unlink($tempname2);


Many other standard command-line functions of many operating systems are available in PHP. This not only includes chmod() and chgrp(), but also the following file system operations:

  • copy() Copies a file

  • rename() Moves a file

  • unlink() Deletes a file

The preceding code duplicates the current file, moves it (or renames it), and finally deletes the resulting file.

Copying, Moving, and Deleting Files (copymovedelete.php)
<?php
  $filename = 'php.ini';
  $tempname1 = $filename . rand();
  $tempname2 = $filename . rand();
  copy($filename, $tempname1);
  echo "Copied to $tempname1<br />";
  rename($tempname1, $tempname2);
  echo "Moved to $tempname2<br />";
  unlink($tempname2);
  echo "File deleted.";
?>


Previous
Table of Contents
Next