note 38968 added to function.copy

From: Date: Tue, 13 Jan 2004 18:01:48 +0000
Subject: note 38968 added to function.copy
Groups: php.notes 
Request: Send a blank email to php-notes+get-63466@lists.php.net to get a copy of this message
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; } ---- Manual Page -- http://www.php.net/manual/en/function.copy.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38968 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38968&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38968&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63466) next »