PHP copy(), rename() and unlink() functions



 PHP copy(), rename() and unlink() file handling

In this php tutorial we will learn how use copy() function, rename() function and unlink() function with php example.

PHP File handling functions :

  • copy() – used to copy a file.
  • rename() – used to rename a file.
  • unlink() – used to delete a file.

PHP copy() Function

The copy() function is used to copy a file.

copy() function syntax

<?php

copy(“source filename”, “destination filename”);

?>

copy() function example

<?php

//copy text file
copy("abc.txt","newabc.txt");

//open ms word .doc file
copy("abc.doc","newabc.doc");

//open pdf file
copy('abc.pdf',"newabc.pdf");

?>

PHP rename() Function

The rename() function is used to rename a file name.

rename() function syntax

<?php

rename(“old filename”,”new filename”);

?>

rename() function example

<?php

//rename txt file
rename("abc.txt","newabc.txt");

//rename ms word .doc file
rename("abc.doc","newabc.doc");

//rename pdf file
rename('abc.pdf',"newabc.pdf");

?>

PHP unlink() Function

How to Delete a file in PHP.

The unlink() function is used to delete a file.

unlink() function syntax

<?php

unlink(“filename”);

?>

unlink() function example

<?php

//delete txt file
unlink("abc.txt");

//delete ms word .doc file
unlink("abc.doc");

//delete pdf file
unlink('abc.pdf");

?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *