SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Italy
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Recursion and transfer data

    Hi all and good PHP programming
    I have create a script that transfer data with ftp function to local folder
    on a remote server.
    I have create a recursive function that read all data on local folder and execute the copy. When i transfer data for little kb the script can be able to copy the data with the same path of local folder e.g C:\\back-up\folder1\file.txt
    copy to /httpdocs/folder1/file.txt it's ok.
    But when the filesize is MB the recursive function is much more speed to read
    that write data and the file not is located on the same hierarchy of local folder.
    THis is the script :



    $user = "user";
    $pasw = "pasw";
    $ftp_server = "ftp.mydomain.xx";
    $mode = "FTP_BINARY";
    $remote_folder = "/httpdocs";

    function ftp_connection() {

    global $user, $pasw, $ftp_server;

    $conn = @ftp_connect($ftp_server);
    $login = @ftp_login($conn, $user, $pasw);

    if((!$conn)||(!$login)) {
    die("Cannot open connect with $ftp_server\n");
    }
    else {
    return $conn;
    }
    }

    $conn = ftp_connection();
    if(!(@ftp_chdir($conn, $remote_folder)))
    die("Cannot to change data folder\n");

    function recursive_dir($dir){


    #Get ID connect ftp_mode and
    global $mode, $conn, $remote_folder;
    #Open resource to read data
    if (!($handle = @opendir($dir)))
    die("Cannot open datadir\n");
    #Read data from local folder $default_dir
    while(false !==($item = @readdir($handle))){

    if (is_dir($dir."/".$item)) {
    #Erase al "." and ".." to content of $item
    if ($item != "." && $item != ".."){
    #Make directory on remote Server
    if (!(@ftp_mkdir($conn, $item)))
    die("Cannot possible mkdir $item\n");
    #Get current directory for current session
    $pwd = @ftp_pwd($conn);
    if(!$pwd) die("Cannot get current Work Directory\n");

    if(!(@ftp_chdir($conn, $pwd."/".$item)))
    die("Cannot change directory:".$pwd."/".$item."\n");


    #Call recursive dir to
    recursive_dir($dir."/".$item);
    if(!(@ftp_chdir($conn, $remote_folder)))
    die("Cannot change directory to $remote_folder");
    }
    }//End for first check of $item: if is_dir($item)
    else {
    $fp = @fopen($dir."/".$item, "r");
    $pwd = @ftp_pwd($conn);
    //echo $pwd."\n";
    if(@ftp_fput($conn, $item, $fp, $mode)){
    //$count++;
    //print $dir."\\".$item."\n";
    }//End for if uploaded file
    fclose($fp);
    }
    }//End for else

    closedir($handle);
    }//End for recursive function

    #Called to recursive function
    recursive_dir($default_dir);
    #Close the connect to FTP Data
    ftp_close($conn);
    ?>




    How to fix the problem ?
    Thanks to all and sorry for my bad English

  2. #2
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Italy
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excuse me i'm a newbie on this forum, now is more readable and much easy
    comprensive.
    I have change ftp_fput() with ftp_put() in this script.
    Thanks to all again
    PHP Code:
    <?php
    //ftp.php


    #Mode transfert data for binary data
    $mode FTP_BINARY;
    #The base path of remote directory
    $remote_folder "/httpdocs";
    #Include data for the connection to the FTP server
    include_once("./config.php");

    #Create a a directory for FTP data called as current date


    function ftp_connection() {

        global 
    $user$pasw$ftp_server;

        
    $conn = @ftp_connect($ftp_server);
        
    $login = @ftp_login($conn$user$pasw);

        if((!
    $conn)||(!$login)) {
           die(
    "Cannot open connect with $ftp_server\n");
        }
        else {
           return 
    $conn;
        }
    }

    $conn ftp_connection();
    if(!(@
    ftp_chdir($conn$remote_folder)))
              die(
    "Cannot to change data folder\n");

    function 
    recursive_dir($dir){


         
    #Get ID connect ftp_mode and
         
    global $mode$conn$remote_folder;
         
    #Open resource to read data
         
    if (!($handle = @opendir($dir)))
                      die(
    "Cannot open datadir\n");
         
    #Read data from local folder $default_dir
         
    while(false !==($item = @readdir($handle))){

              if (
    is_dir($dir."/".$item)) {
                  
    #Erase al "." and ".." to content of $item
                  
    if ($item != "." && $item != ".."){
                     
    #Make directory on remote Server
                     
    if (!(@ftp_mkdir($conn$item)))
                              die(
    "Cannot possible mkdir $item\n");
                     
    #Get current directory for current session
                     
    $pwd = @ftp_pwd($conn);
                     if(!
    $pwd) die("Cannot get current Work Directory\n");

                     if(!(@
    ftp_chdir($conn$pwd."/".$item)))
                                     die(
    "Cannot change directory:".$pwd."/".$item."\n");

                     
                     
    #Call recursive dir to
                     
    recursive_dir($dir."/".$item);
                     if(!(@
    ftp_chdir($conn$remote_folder)))
                                     die(
    "Cannot change directory to $remote_folder");
                    }
             }
    //End for first check of $item: if is_dir($item)
             
    else {
                   
    //$fp = @fopen($dir."/".$item, "r");
                     
    $pwd = @ftp_pwd($conn);

                     if(@
    ftp_put($conn$item$dir."/".$item$mode)){
                       }
    //End for if uploaded file
                    //fclose($fp);
                 
    }
         }
    //End for else

         
    closedir($handle);
    }
    //End for recursive function

    #Called to recursive function
    recursive_dir($default_dir);
    #Close the connect to FTP Data
    ftp_close($conn);
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •