in reply to Re: Net::ftp - copy of file in cgi-bin
in thread Net::ftp - copy of file in cgi-bin

Thanks very much for your interesting reply.

I have put the script on the server onto which I am downloading the files. Based on my undertanding of the Net:: FTP docs the new file should be created in and identical path/$filename as the original, but in fact the file is only showing up in the cgi-bin. Obviously it is my misunderstanding but I would appreciate some help in unravelling this. (Obviously having got the files onto the server I can easily shift them to where I need them and clean up afterwards, but I would prefer to get this working to my satisfaction!).

my $ftp = Net::FTP->new("$home") or die "Can't connect: 1 $@\n"; $ftp->login($username, $password) or die "Couldn't login - 1\n"; $ftp->cwd($directory) or die "Couldn't change direct +ory - 1\n"; $ftp->get($filename) or die "Couldn't get $filename + - 1\n"; $ftp->put($filename) or die "Couldn't put $filename +\n"; my @lines = $ftp->ls("/data/edit");

Replies are listed 'Best First'.
Re: Re: Re: Net::ftp - copy of file in cgi-bin
by ChrisR (Hermit) on Oct 28, 2003 at 18:19 UTC
    According to the docs:
    The FTP protocol allows files to be sent to or fetched from the server. Each transfer involves a local file (on the client) and a remote file (on the server). In this module, the same file name will be used for both local and remote if only one is specified. This means that transferring remote file /path/to/file will try to put that file in /path/to/file locally, unless you specify a local file name.
    Update:
    After taking a closer look at the docs, I found:
    get ( REMOTE_FILE [, LOCAL_FILE , WHERE] )
    Get REMOTE_FILE from the server and store locally. LOCAL_FILE may be a filename or a filehandle. If not specified, the file will be stored in the current directory with the same leafname as the remote file.
    If WHERE is given then the first WHERE bytes of the file will not be transfered, and the remaining bytes will be appended to the local file if it already exists.
    Returns LOCAL_FILE, or the generated local file name if LOCAL_FILE is not given. If an error was encountered undef is returned.

    put ( LOCAL_FILE , REMOTE_FILE )
    Put a file on the remote server. LOCAL_FILE may be a name or a filehandle. If LOCAL_FILE is a filehandle then REMOTE_FILE must be specified. If REMOTE_FILE is not specified then the file will be stored in the current directory with the same leafname as LOCAL_FILE.
    Returns REMOTE_FILE, or the generated remote filename if REMOTE_FILE is not given.
    NOTE: If for some reason the transfer does not complete and an error is returned then the contents that had been transfered will not be remove automatically.
      Yep, that's the bit I read - so can you shed some light on why it is writing the file to the cgi-bin? It is certainly going to right place on the external server and copying the correct file - but it is not duplicating it on the 'home' server. I'm a bit at a loss, I must admit...

      Update: Whoops - I answered this and then saw your updates! I shall go and study them now. I appreciate your help!

        I'm going to hazard a guess that cgi-bin is where the script resides?

        If so, and if you are not specifying an absolute path to your final destination, you could be pulling the file into a default location, which is the directory where the script resides. Or in the case of a cron job, the users home directory. Now, you could do a chdir() prior to invoking Net::FTP and put your process into the destination directory. Or, you might be able to specify where you want the file in your "GET" statement. Since you are only involving two servers, there really does not seem to be a need to do two FTP's.

        You might try something like this:

        chdir("/local/destination/for/file") or die "Cannot chdir $!"; $ftp->get("myfile.txt","my_new_file.txt");
        or
        $ftp->get("/remote/path/to/file/myfile.txt","/local/path/to/file/myfil +e.txt");
        Does that help any? (I think I'm understanding what you are trying to do :)


        "Ex libris un peut de tout"
        As far as the FTP object is concerned, there is no path to the file. When you execute the cwd, you set the relative path for the object. I found some interesting stuff in the docs and placed in my previous post.

        On another note: If you have moved the script to the backup server, you should only need one ftp instance and then one get statement per file. If the script is on the production machine, you still only need one ftp instance and one put statement per file.