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."; ?> 




PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net