user3227965
user3227965

Reputation: 23

Copy files with certain extension php4

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

Answers (2)

user3227965
user3227965

Reputation: 23

I used xcopy inside "system" php tags to copy instead.

Upvotes: 0

Ben Fortune
Ben Fortune

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

Related Questions