Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate a PHP file using PHP

Tags:

file

php

How can I duplicate a php file that I already created and place it into a different directory when a user pushes the submit button

like image 312
HELP Avatar asked Oct 06 '10 07:10

HELP


People also ask

What is use of copy () function?

Definition and Usage The copy() function copies a file. Note: If the to_file file already exists, it will be overwritten.

How do you copy & delete a files in PHP by using copy and unlink?

PHP File handling functions :copy() – used to copy a file. rename() – used to rename a file. unlink() – used to delete a file.


1 Answers

You can use the copy function as:

if ( copy($srcFilename,$destPath) ) {
  // file copied.
} else {
  // error occurred..call error_get_last() function for err details.      
}

Few things to note:

  • If the destination file exists, copy will overwrite it. If you don't want this you can check the existence of the destination file using file_exists function before you copy.

  • Both the parameters of copy must be files. In Linux we usually do: cp file dir to copy the file file into the directory dir with the name file. This will not work with copy.

  • Some hosting companies disable copy function for security reasons. In that case you can implement your own copy by reading the file using file_get_contents and writing to the file using file_put_contents. Since you want to copy PHP scripts (which are not very large memory wise) this will work fine.

like image 187
codaddict Avatar answered Nov 03 '22 08:11

codaddict