How to copy a file from one directory to another using PHP?

Click Upvote picture Click Upvote · Apr 24, 2011 · Viewed 285.9k times · Source

Say I've got a file test.php in foo directory as well as bar. How can I replace bar/test.php with foo/test.php using PHP? I'm on Windows XP, a cross platform solution would be great but windows preferred.

Answer

Pascal MARTIN picture Pascal MARTIN · Apr 24, 2011

You could use the copy() function :

// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');


Quoting a couple of relevant sentences from its manual page :

Makes a copy of the file source to dest.

If the destination file already exists, it will be overwritten.