php mirror a folder/site

Discussion in 'PHP' started by Edynas, Sep 28, 2007.

  1. #1
    I have this piece of code that will list all files and subfolders in a folder. I want to take those values and create the same structure and files in an other folder. Mirroring the folder

    function getFileList($dir, $recurse=false)
    {
    # array to hold return value
    $retval = array();

    # add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    # open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
    # skip hidden files
    if($entry[0] == ".") continue;
    if(is_dir("$dir$entry")) {
    $retval[] = array(
    "name" => "$dir$entry/",
    "type" => filetype("$dir$entry"),
    "size" => 0,
    "lastmod" => filemtime("$dir$entry")
    );
    if($recurse && is_readable("$dir$entry/")) {
    $retval = array_merge($retval, getFileList("$dir$entry/", true));
    }
    } elseif(is_readable("$dir$entry")) {
    $retval[] = array(
    "name" => "$dir$entry",
    "type" => "",
    "size" => filesize("$dir$entry"),
    "lastmod" => filemtime("$dir$entry")
    );
    }
    }
    $d->close();

    return $retval;
    }

    $dirlist = getFileList("./", true);
    -----

    i was thinking i should do something like

    foreach($dirlist as $file) {

    $originalfile = $file['name'];
    copy it to new location
    }

    but get stuc how i know the new location
    any help would be appriciated
     
    Edynas, Sep 28, 2007 IP
  2. msaqibansari

    msaqibansari Peon

    Messages:
    84
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    function full_copy( $source, $target )
        {
            if ( is_dir( $source ) )
            {
                @mkdir( $target );
               
                $d = dir( $source );
               
                while ( FALSE !== ( $entry = $d->read() ) )
                {
                    if ( $entry == '.' || $entry == '..' )
                    {
                        continue;
                    }
                   
                    $Entry = $source . '/' . $entry;           
                    if ( is_dir( $Entry ) )
                    {
                        full_copy( $Entry, $target . '/' . $entry );
                        continue;
                    }
                    copy( $Entry, $target . '/' . $entry );
                }
               
                $d->close();
            }else
            {
                copy( $source, $target );
            }
        }
    
    ?>
    Code (markup):
    http://cn.php.net/manual/en/function.copy.php
     
    msaqibansari, Sep 28, 2007 IP
    Edynas likes this.
  3. Edynas

    Edynas Peon

    Messages:
    796
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you so much
     
    Edynas, Sep 29, 2007 IP