note 38968 deleted from function.copy by tomsommer
| From: | tomsommer@php.net | Date: | Tue, 13 Jan 2004 19:24:19 +0000 |
| Subject: | note 38968 deleted from function.copy by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63470@lists.php.net to get a copy of this message | ||
Note Submitter: objobj@hotmail.com
----
Well...I have realized that the script of torrmal at yahoo dot com published at 04-Jul-2003 11:05
copies the content of $wf to $wto, but NOT MOVES THE COMPLETE $wf to $wto.
I mean the operation :
-$wto
|--$wf
is not made!!!
Imagine
-D
|--Cinematica
|--0.txt
|--1.txt
|--2.txt
|--a
|--font
|--3.txt
In short:
D:/Cinematica is a folder which contains 3 files txt and a folder called a.
D:/Cinematica/a contains a folder called font
D:/Cinematica/a/font contains a file called 3.txt
Now, I want to copy D:/Cinematica in D:/Mecanica (D:/Mecanica can be exist or not).
The previous code make this operation:
-D
|--Mecanica
|--0.txt
|--1.txt
|--2.txt
|--a
|--Font
|--3.txt
BUT I WANT THIS STRUCTURE:
-D
|--Mecanica
|--Cinematica
|--0.txt
|--1.txt
|--2.txt
|--a
|--Font
|--3.txt
So I had to improve the code and the result is this (and WORKS !)
Example to test://
$origen="D:/cinematica";
$destino="D:/neroerror/";
$i=0; //variable $i represents the first time to call the function (recursive calls are $i=1)
//
cp($origen,$destino,$i);
function cp($wf, $wto,$i){ // it moves $wf to $wto
if ($i==0){
$wto=$wto.basename($wf);
if (!file_exists($wto)){
mkdir($wto,0777);
}
}
if ((!file_exists($wto)) AND ($i!=0)){
mkdir($wto,0777);
}
$arr=ls_a($wf);
foreach ($arr as $fn){
echo "<br>el fn".$fn;
if($fn){
$fl="$wf/$fn";//D:/cinematica/a/
$flto="$wto/$fn";//D:/neroerror/a/
//$flto="$wto/".basename($fl);//D:/neroerror/cinematica/a/
echo "<br>el flto".$flto;
if(is_dir($fl)) cp($fl,$flto,1);//RECURSIVE CALLS $i=1
else copy($fl,$flto);
}
}
}
///////////////////////////////////////////////////
/// ls_a function////////////////////////
// This function lists a directory.
// ANd is needed for the cp function.
function ls_a($wh){
if ($handle = opendir($wh)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." ) {
if(!$files) $files="$file";
else $files="$file\n$files";
}
}
closedir($handle);
}
$arr=explode("\n",$files);
for($i=0;$i<count($arr);$i++){
echo "<br>$arr[$i]";
}
return $arr;
}