Reputation: 69
I have a text file that I want to copy into all sub directories which have the following structure.
S000314/0000356/data folder
/0000357/data folder
/0000358/data folder
So, I am reading all the sub directories and trying to copy the text file into all of them..But, rename function can copy only once.its copying the text file into 0000356->data folder
if ($handle = opendir('S000314')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..")
{
rename("sdata.txt" , "/S000314/$entry/data/sdata.txt");
}
}
closedir($handle);
}
I have no clue on how to copy the same file into all of the sub directories.Please take a look at the code below.
Upvotes: 0
Views: 722
Reputation: 10658
I'd recommend using copy()
instead of rename()
because that is the actual operation you want (the code will be easier to read).
A problem could be the directory separator - I tested it on Windows where both /
and \
seems to be working, but that shouldn't be your concern, as PHP has DIRECTORY_SEPARATOR
.
Now this code worked on my machine:
$topdir = 'S000314';
if ($handle = opendir($topdir)) {
print "in\n";
while (FALSE !== ($subdir = readdir($handle))) {
if ($subdir != '.' && $subdir != '..') {
$dest = $topdir . DIRECTORY_SEPARATOR .
$subdir . DIRECTORY_SEPARATOR
'data' . DIRECTORY_SEPARATOR;
copy('data.txt', $dest.'data.txt');
}
}
}
Upvotes: 1
Reputation: 2793
@copy($yourSource_file, $yourFile_element . '/youPage.html');
Upvotes: 0