Need Help With FTP script

Discussion in 'PHP' started by hawkseye, Dec 30, 2005.

  1. #1
    I have written a sript in php which downloads all files and folders from a folder at remote server to a folder at my local server. The script would run from my local Server. The first few passes go ok. but when the script return back to the root (the folder it started from) and starts to copy files from there it gives error No such file or directory. The output of paths r ok. Cant figure out whats wrong. Please help me.


    <?php
    $NORMLS_SERVER = "Remote Server";
    $ftpDirPath = "/Remote Folder/";
    $localPath = "Local Folder";

    //connecting to normls server
    if(! $ftp = ftp_connect($NORMLS_SERVER))
    {
    fwrite($error_log, "Couldnt Connect to NORMLS SERVER .... update data ABORTED !");
    die("Couldnt Connect to NORMLS SERVER .... update data ABORTED !");
    }
    //logging in

    if(! ftp_login($ftp, "user", "password"))
    {
    fwrite($error_log, "Couldn't Log in to the NORMLS server ! ... update data ABORTED !");
    die("Couldn't Log in to the NORMLS server ! ... update data ABORTED !");
    }

    allFilesNFolders($ftp, $ftpDirPath, $localPath);

    function allFilesNFolders($ftp, $ftpDirPath, $localPath){
    ftp_chdir($ftp, $ftpDirPath);
    $contents = ftp_nlist($ftp, ".");
    for($i = 0; $i < count($contents); $i++)
    {
    $path = $localPath . "/" . $contents[$i];
    echo "<br>" . $path;
    echo "<br>" . $ftpDirPath;
    $tmp = explode(".", $contents[$i]);
    if (count($tmp) == 1){
    mkdir($path, 0777) or die("Directory create function failed !! ... ABORTING !!");
    allFilesNFolders($ftp, $ftpDirPath . $contents[$i], $path);
    }
    else{
    $fpPic = fopen($path, "w");
    ftp_fget ($ftp, $fpPic, $contents[$i], FTP_BINARY);
    fclose($fpPic);
    }
    }
    }
    ?>
     
    hawkseye, Dec 30, 2005 IP
  2. hawkseye

    hawkseye Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Figured out what was wrong


    function allFilesNFolders($ftp, $ftpDirPath, $localPath){
    ftp_chdir($ftp, $ftpDirPath);
    $contents = ftp_nlist($ftp, ".");
    for($i = 0; $i < count($contents); $i++)
    {
    $path = $localPath . "/" . $contents[$i];
    $remotePath = $ftpDirPath . "/" . $contents[$i];

    $tmp = explode(".", $contents[$i]);
    if (count($tmp) == 1){
    mkdir($path, 0777) or die("Directory create function failed !! ... ABORTING !!");
    allFilesNFolders($ftp, $remotePath, $path);
    }
    else{
    $fpPic = fopen($path, "w");
    ftp_fget ($ftp, $fpPic, $remotePath, FTP_BINARY);
    fclose($fpPic);
    }
    }
    }


    The problem was in remote path when it returned to the root directory after saving all the files.
     
    hawkseye, Dec 30, 2005 IP
  3. arnek

    arnek Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    This is quite intesting, I also finished up a FTPTransferer class just a while back.... isn't FTP just the best for file transfers
     
    arnek, Jan 13, 2006 IP
  4. Shoemoney

    Shoemoney $

    Messages:
    4,474
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #4
    but rsync is so much easier or scp
     
    Shoemoney, Jan 13, 2006 IP
  5. arnek

    arnek Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    i've never heard of using that from within PHP ... mmmm
     
    arnek, Jan 13, 2006 IP
  6. Shoemoney

    Shoemoney $

    Messages:
    4,474
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #6
    I was more responding to your comment that ftp was the best for file transfers.
     
    Shoemoney, Jan 13, 2006 IP