简体   繁体   中英

How to copy a file to multiple directories, PHP?

copy('foo/test.php', 'bar/test.php');

Above is the code which will transfer the test.php file from foo to bar and so overwrite any files if necessary.

I am trying to achieve the above but with several, undefined directories. How would I go about doing so?

Overall

  • There are a bunch of directories in one location (an undefined amount)
  • I need to replace the test.php files in those directories with the new one that has been copies from foo .

I would appreciate any help, thanks!

as written you can pass one array in arguments and loops on it like that :

function fullCopy($oldDir = '', $newDir = []) {
    foreach ($dir as $value){
        if ( is_dir($oldDir && is_dir($newDir)) ) {
            copy( $oldDir . '/test.php', $newDir . '/test.php');
        }
    }
}
fullCopy('foo', ['bar', 'bar2', 'bar3']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM