note 27867 modified in function.copy by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:55:30 +0000 |
| Subject: | note 27867 modified in function.copy by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80077@lists.php.net to get a copy of this message | ||
As already noted, the rec_copy function only work(ed)(s) on the "first" level and then
dies .. naturally the problem was/is rather simple.. as it failed to account for the trailing
slash(es) of the directories..
Another issue to consider, when working with a recursive function, is the possiblity of copying to a
destination that lies within the source path.. i.e. from=./tmp to=./tmp/test
given the manner in which rec_copy operates, it writes the destination directory, then reads from
the source.. you wind up with a tree where /test is duplicated endlessly.
i.e ./tmp/test/test/test/test/test/...................
perhaps obvious to the veteran programmer, but not quite so obvious to the newcomer.
the simplest solution - looking at the sample - would be to prevent the copy if the destination lies
within the source..
one way might be to ...
<?php
$match = ereg_replace("/","\\/",$from);
if (!@preg_match("/^".$match."/",$to)) {
//call the copy function
}
else {
//output error response as source and destination are same
}
?>
--was--
As already noted, the rec_copy function only work(ed)(s) on the "first" level and then
dies .. naturally the problem was/is rather simple.. as it failed to account for the trailing
slash(es) of the directories..
Another issue to consider, when working with a recursive function, is the possiblity of copying to a
destination that lies within the source path.. i.e. from=./tmp to=./tmp/test
given the manner in which rec_copy operates, it writes the destination directory, then reads from
the source.. you wind up with a tree where /test is duplicated endlessly.
i.e ./tmp/test/test/test/test/test/...................
perhaps obvious to the veteran programmer, but not quite so obvious to the newcomer.
the simplest solution - looking at the sample - would be to prevent the copy if the destination lies
within the source..
one way might be to ...
$match = ereg_replace("/","\\/",$from);
if (!@preg_match("/^".$match."/",$to)) {
//call the copy function
}
else {
//output error response as source and destination are same
}
http://php.net/manual/en/function.copy.php