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
Definition and Usage The copy() function copies a file. Note: If the to_file file already exists, it will be overwritten.
PHP File handling functions :copy() – used to copy a file. rename() – used to rename a file. unlink() – used to delete a file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With