Copying, Renaming, and Deleting Files

PHP also allows you to copy, rename, and delete files. The functions to perform these operations are copy() , rename() , and unlink() , respectively.

The copy() function takes two string arguments: the first argument is the path to the file to copy, and the second argument is the path to copy it to. It returns true if the file was successfully copied, or false if there was a problem copying the file.


The following example copies the source file phpcopy.txt to the destination file phpcopied.txt in the same folder:

copy( “./copyme.txt”, “./phpcopied.txt” );

The rename() function is used to rename (or move) a file. It works in much the same way as copy() .


rename( “./”, “./conent.backup” );

To move a file to a different folder use:

rename( “/desktop/php/phpdata.txt”, “/desktop/php/php5.6/phpdata.txt” );

The unlink() function lets you delete files from the server. To use it, pass the filename of the file you want to delete.


if you want to say Welcome to the file phpdata.txt in the current directory, you could write:

unlink( “./phpdata.txt” );

Scroll to Top