Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy and rename directory

Status
Not open for further replies.

blekfis

Programmer
Oct 19, 2001
38
0
0
SE
I've got a directory called "alfa" that contains both files of various types and subdirectories with files. I'd like to be able to create a new directory named "beta" and copy all the files and subdirectories from "alfa" into "beta". All should be done in PHP...

Is it possible (dumb quetion!) and how to do it?

//N!cklas
 
this script is taken from the php manual. i have no idea whether it works but it looks ok
Code:
////////////////////////////////////////////////////////
/// cp function/////////////////////////////////
////////////////////////////////////////////////////////
       function cp($wf, $wto){ // it moves $wf to $wto
               mkdir($wto,0777);
               $arr=ls_a($wf);
               foreach ($arr as $fn){
                      if($fn){
                               $fl="$wf/$fn";
                               $flto="$wto/$fn";
                        if(is_dir($fl)) cp($fl,$flto);
                               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);
         return $arr;
       }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top