How to duplicate a PHP file using PHP
How can I duplicate a php fi开发者_开发问答le that I already created and place it into a different directory when a user pushes the submit button
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 usingfile_exists
function before you copy.Both the parameters of copy must be files. In Linux we usually do:
cp file dir
to copy the filefile
into the directorydir
with the namefile
. This will not work withcopy
.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 usingfile_put_contents
. Since you want to copy PHP scripts (which are not very large memory wise) this will work fine.
精彩评论