vallez
vallez

Reputation: 117

how to copy php file multiple file name using php?

i want to copy same php file using php , working with copying one file, I can copy one file , but I have problem when copying multi files using array.

$copys = file('copy.txt');

foreach($copys as $copy) {
copy('page1.php', '$copy');
}

copy.txt is name files:

page2.php
page3.php
page4.php

i want to copy page1 to page2, page3, page4 ,.. page100

But this code not work !

Could you give me a solution :(

Thanks for any help !

Upvotes: 1

Views: 145

Answers (2)

ptvty
ptvty

Reputation: 5664

Do something like this:

$copys = arary('from.txt'=>'to.txt', 'from2.txt'=>'to2.txt');

foreach($copys as $from => $to) {
   copy($form, $to);
}

Or if you want to copy the same file multiple times

$copys = file('copy.txt');

foreach($copys as $to) {
   copy('page1.php', $to.".php");
}

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94672

If you want to generate a specific number of copies and the numbers for the new file names you will have to use a for loop

<?php
    $master = 'page1.txt';
    $copy_to = 'page%d.txt';
    $num_copies = 10;

    // start you rloop at 2 so we start copying to `page2.txt`
    // and dont overwrite page1.txt
    for ($i=2; $i < $num_copies+2; $i++) {
        copy($master, sprintf($copy_to, $i));
    }

Upvotes: 2

Related Questions