Reputation: 23
I am writing a small script to copy files with a certain extension in a directory to another directory. At the moment I have:
<?php
$targetDir = "D:\\test dir\\";
$files = glob("C:\\old dir\\*.txt");
foreach ($files as $tocopy) {
copy($tocopy, $targetDir);
}
?>
However this does not seem work. I can print_r the files and it finds them, but does not copy over. How can I copy over files with.txt extension into a different directory?
Upvotes: 2
Views: 133
Reputation: 32127
You need to add the filename to the target too. Something similar to the following should help.
$targetDir = "D:\\test dir\\";
$files = glob("C:\\old dir\\*.txt");
foreach ($files as $file) {
copy($file, $targetDir . $file);
}
Upvotes: 2