あなたが現在行っていることは、最初の反復で「test.txt」であるソースファイルをループしてから、宛先配列をループしてコピー機能を2回実行することです:
folder1/test.txt を使用した最初の反復
- copy("folder1/test.txt", "folder2/test.txt");
- copy("folder1/test.txt", "folder2/test2.txt";
folder1/test2.txt を使用した 2 回目の反復:
- copy("folder1/test2.txt", "folder2/test.txt");
- copy("folder1/test2.txt", "folder2/test2.txt";
最後に、両方のファイルを $source 配列の最後のファイルで上書きしました。したがって、「folder2」の両方のファイルには、test2.txt のデータが含まれています。
あなたが探しているものは次のとおりです。
foreach($sourcefiles as $key => $sourcefile) {
copy($sourcefile, $destinations[$key]);
}
上記の例では、$sourcefile は $sourcefiles[$key] と同じです。
これは、PHP が値にキーを自動的に割り当てるという事実に基づいています。$sourcefiles = array('file1.txt', 'file2.txt'); 次のように使用できます。
$sourcefiles = array(
0 => 'file1.txt',
1 => 'file2.txt'
);
もう 1 つのオプションは、配列の 1 つの長さを for ループで使用することです。これは同じことを別の方法で行います。
for ($i = 0; $i < count($sourcefiles); $i++) {
copy($sourcefiles[$i], $destinations[$i]);
}