mchiles has asked for the wisdom of the Perl Monks concerning the following question:

O wise ones and those seeking wisdom...

I have been trying for a couple of weeks now to get the put function working in a subscript using Net::FTP. Here is the code:

use Net::FTP; my $ftp; $ftp = Net::FTP->new($ftp_server, Debug => 1)or die "Cannot connect"; $ftp->login($login,$password)or die $ftp->message; $ftp->pwd or die $ftp->message; $ftp->cwd($upload_to_dir) or die $ftp->message; warn "Failed to set binary mode\n" unless $ftp->binary(); print "Uploading $input_file...<br>"; $ftp->put ( $input_file, $new_file_name ) or die "did not upload"; $ftp->quit;

The file never uploads at the $ftp->put line - there is only death.

My first guess is that the file path is formatted wrong for the $input_file variable. My current value and format is: e:\test\picture.jpg

I have also tried
e:/test/picture.jpg
e://test//picture.jpg
E://test//picture.jpg

and a variety of other such things but I haven't got it to work. Does anyone know what the correct format for a path for that arguement should be?

Do I need to somehow direct the script to a specific directory on the local computer first (similar to how $ftp->cwd() is used to get to the proper directory on the remote host)?

My value for $new_file_name is test.jpg.

I have tested other things such as creating a directory using this script and I can do that, so I believe I am correctly logged in by FTP and in the proper directory.

I have talked to my host to see that Net::FTP is correctly installed and they assure me that it is, and even reinstalled it to be sure. I have the log of that re-installation if anyone things it would be useful.

I noticed on my server in the list of Installed Perl Modules that there appears to be two copies of Net::FTP - could that be causing my problem, if one is defective??? If that is the case, how do we remove one?

Could it be that the Net::FTP program is not compatible with my server (basic Linux) or its firewall? I seem to have no trouble ftping with the same username and password via ws_ftp pro.

Your insight and wisdom is greatly appreciated!

-Matt :)

Appended Update:

Many thanks to the brothers and sisters here at the monastery, who have enlightened my path! You have solved my problem - and the solution (for those who may have a similar problem) is that that is not what Net::FTP is designed to do. I don't have an answer yet for how to do what I am want to do, but I'll check in a different direction and I'm sure with Perl a way will be found!

-Matt :)

Replies are listed 'Best First'.
Re: Struggles with Put in Net::FTP
by jaa (Friar) on May 25, 2004 at 18:14 UTC
    You need to get more info on what is causing the failure
    $ftp->put ( $input_file, $new_file_name ) or die "did not upload $inpu +t_file to $new_file_name: " . $ftp->message;
    Maybe the problem is in $new_file_name?
    use File::Basename wq(basename); my $new_file_name = basename($input_file); die "no such file: $input_file" unless -s $input_file; $ftp->put ( $input_file, $new_file_name ) or die "did not upload $inpu +t_file to $new_file_name: " . $ftp->message;
    Test it against your own FTP server running with verbose logging and see what the FTP server logs report?

    Regards,

    Jeff

      Try e:\\test\\picture.jpg
      Jeff-

      You may be on the right track...

      Maybe the problem is in $new_file_name?
      use File::Basename wq(basename); my $new_file_name = basename($input_file); die "no such file: $input_file" unless -s $input_file;
      The first two lines of this gave me problems, with the error:
      Undefined subroutine &main::wq called at /home/auctionb/public_html/cgi-bin...bla, bla

      But the third line (the die line) got me an interesting error message (when the first two lines were commented out):

      no such file: e:\test\picture.jpg at /home/auctionb/public_html/cgi-bin/auction/bulklister.pl line 280.

      It looks to me, based on this, that it can't find my input file, e:\\test\\picture.jpg. So why is that? It looks to me that for some reason the script (at the host) is not looking for the file at my local computer... ideas?

      You also advised:
      Test it against your own FTP server running with verbose logging and see what the FTP server logs report?
      You will have to excuse my technical ignorance here, but I am not sure what you mean or how to do it. :(

      -Matt :)
        If I understood this correctly, the script using Net::FTP is on a linux machine, your webhost, and the file you would like to upload (to where?) is on your home PC?? Even if you are running the script as a CGI from your home PC via a browser, it is still running locally on the linux system, and will NOT be able to see your PC files.

        To copy files from your home PC to somewhere else via such a CGI, you'll need to use the CGI upload functions to upload the file from the PC to the linux server first, then use Net::FTP to copy it elsewhere.

        C.

Re: Struggles with Put in Net::FTP
by nimdokk (Vicar) on May 25, 2004 at 17:53 UTC
    Have you tried formatting the path using File::Spec? Other than that possibility, there should be no problem using Net::FTP with Linux, FTP is simply a protocal that does not care about the platform (as long as the server is running). You might be on to something if there are two copies of Net::FTP installed. You might look in the lib and site\lib directories to see if there are duplicates and try uninstalling Net::FTP and reinstalling it.
      I have not tried formatting the path using File::Spec. I have looked through cpan on File::Spec but am unsure how to use it to be beneficial to me... Could you possibly provide an example?

      Good to hear that FTP should be no trouble with Linux (makes sense and I didn't think that would be it).

      I'll check more into duplicates and let you know... Thanks for your help!

      -Matt :)
        OK, to make use of File::Spec, look at the following sample:

        use File::Spec; my $file_path=File::Spec->catfile('C:','Path','To','File.exe'); print "$file_path\n";

        This will print:

        C:\Path\To\File.exe

        The advantage is, the module will put in the directory seperators for you, as appropriate for the OS. Also, you don't have to remember to escape the '\' character in a Windows path (basically making things cleaner). Also, it looks like you've got debug turned on, what kind of messages is it Net::FTP giving you?

Re: Struggles with Put in Net::FTP
by vek (Prior) on May 25, 2004 at 21:40 UTC
    I see you have turned on debugging, could you show us the output? Are you sure that $input_file and $new_file_name are defined? Use $ftp->message() to get the error message if the $ftp->put() fails.

    $ftp->put($somefile, $some_other_file) || die $ftp->message(), "\n";
    -- vek --
      Yes, $input_file and $new_file_name are defined. When I do the following:

      print "Input File: $input_file<br>Output File: $new_file_name";
      I get the following output:
      Input File: e:/test/picture.jpg
      Output File: 1108853442.jpg
      Using the code you list $ftp->put($input_file, $new_file_name) || die $ftp->message(), "\n"; I don't get much error, just this, which is pretty worthless except for confirming the line the error is on:
      at /home/auctionb/public_html/cgi-bin/auction/bulklister.pl line 283
      As far as I know it is serverside stuff that controls those errors - stuff I unforunately don't have control over, and my server is unwilling to hook up more detailed error reporting. :( If you know of another way I can force more detailed error reporting without being the server administrator I am all ears! :)

      -Matt :)