开发者

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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜