tags:

views:

1364

answers:

3

I am trying to upload multiple files via Net::FTP and Perl. Has anyone done this as even my basic script below fails?

use Net::FTP;
use File::Basename;

my $ftp;
my $host ='56.309.24.2';
my $user ='user';
my $pw   ='pass';

my $file ='097360718843.jpeg';
my $path ='public_html/uploaded/product_images';

chomp($host,$user,$pw,$path, $file);

$ftp=Net::FTP->new($host) or die "could not login";
$ftp->login($user,$pw) or die "could not login";
$ftp->cwd($path) or die "could not cwd $path";
$ftp->ls;
$ftp->put($file) or die "could not put $file";
$ftp->site("chmod 600 " . basename($file));

Here's a log of the transfer:

Net::FTP>>> 
Net::FTP(2.75) 
Net::FTP>>> Exporter(5.58) 
Net::FTP>>> 
Net::Cmd(2.26) 
Net::FTP>>> IO::Socket::INET(1.27) 
Net::FTP>>> IO::Socket(1.28) 
Net::FTP>>> IO::Handle(1.24) 
Net::FTP=GLOB(0x180c6a8)<<< 220---------- Welcome to Pure-FTPd [TLS] ---------- 
Net::FTP=GLOB(0x180c6a8)<<< 220-You are user number 1 of 50 allowed. 
Net::FTP=GLOB(0x180c6a8)<<< 220-Local time is now 16:19. Server port: 21. 
Net::FTP=GLOB(0x180c6a8)<<< 220-IPv6 connections are also welcome on this server. 
Net::FTP=GLOB(0x180c6a8)<<< 220 You will be disconnected after 15 minutes of inactivity.
Net::FTP=GLOB(0x180c648)>>> user user_name 
Net::FTP=GLOB(0x180c648)<<< 331 User user_name OK. Password required 
Net::FTP=GLOB(0x180c648)>>> PASS .... 
Net::FTP=GLOB(0x180c648)<<< 230-User user_name has group access to: user_name wheel 
Net::FTP=GLOB(0x180c648)<<< 230 OK. Current restricted directory is / 
Net::FTP=GLOB(0x180c648)>>> CWD public_html/uploaded/product_images/ 
Net::FTP=GLOB(0x180c648)<<< 250 OK. Current directory is /public_html/uploaded/product_images 
Net::FTP=GLOB(0x180c648)>>> PORT 192,168,1,10,200,38 
Net::FTP=GLOB(0x180c648)<<< 200 PORT command successful
Net::FTP=GLOB(0x180c648)>>> NLST 
Net::FTP=GLOB(0x180c648)<<< 150 Connecting to port 50703 
Net::FTP=GLOB(0x180c648)<<< 226-Options: -a 
Net::FTP=GLOB(0x180c648)<<< 226 Output truncated to 2000 matches 
Net::FTP=GLOB(0x180c648)>>> ALLO 7903 
Net::FTP=GLOB(0x180c648)<<< 200 Zzz... 
Net::FTP=GLOB(0x180c648)>>> PORT 192,168,1,10,200,39 
Net::FTP=GLOB(0x180c648)<<< 200 PORT command successful 
Net::FTP=GLOB(0x180c648)>>> STOR 097360718843.jpeg 
Net::FTP=GLOB(0x180c648)<<< 150 Connecting to port 50704 
Net::FTP=GLOB(0x180c648)<<< 226-File successfully transferred
Net::FTP=GLOB(0x180c648)<<< 226 0.381 seconds (measured here), 20.21 Kbytes per second 
Net::FTP=GLOB(0x180c648)>>> SITE chmod 600 097360718843.jpeg 
Net::FTP=GLOB(0x180c648)<<< 200 Permissions changed on 097360718843.jpeg
+2  A: 

Use

my $ftp = Net::FTP->new($host, Debug => 1) 
    or die "Could not create FTP object: $@";

Additional good practice:

  • Make sure you have

    use strict;
    use warnings;
    
  • Don't declare variables before they are needed.

  • Include the error in the error messages.

In short, try the following script:

#!/usr/bin/perl

use strict;
use warnings;

use Net::FTP;

my $host ='56.309.24.2';
my $user ='user';
my $pw   ='pass';

my $file ='097360718843.jpeg';
my $path ='public_html/uploaded/product_images';

my $ftp = Net::FTP->new($host, Debug => 1) 
    or die "Could not connect to '$host': $@";

$ftp->login($user, $pw) 
    or die sprintf "Could not login: %s", $ftp->message;

$ftp->cwd($path) 
    or die sprintf "Could not login: %s", $ftp->message;

$ftp->ls;

$ftp->binary;

$ftp->put($file) 
    or die die sprintf "Could not login: %s", $ftp->message;

$ftp->site("chmod 600 $file");
Sinan Ünür
Ok I got some output. Im logged into the server so the connection is made but no upload.
tin tin
@unknown (google): You should provide the debug output.
Sinan Ünür
Net::FTP>>> Net::FTP(2.75)Net::FTP>>> Exporter(5.58)Net::FTP>>> Net::Cmd(2.26)Net::FTP>>> IO::Socket::INET(1.27)Net::FTP>>> IO::Socket(1.28)Net::FTP>>> IO::Handle(1.24)Net::FTP=GLOB(0x180c6a8)<<< 220---------- Welcome to Pure-FTPd [TLS] ----------Net::FTP=GLOB(0x180c6a8)<<< 220-You are user number 1 of 50 allowed.Net::FTP=GLOB(0x180c6a8)<<< 220-Local time is now 16:19. Server port: 21.Net::FTP=GLOB(0x180c6a8)<<< 220-IPv6 connections are also welcome on this server.Net::FTP=GLOB(0x180c6a8)<<< 220 You will be disconnected after 15 minutes of inactivity.
tin tin
Try `$ftp->binary` so you are not trying to upload a binary file in ASCII mode. Also, are you running the exact script above or have you made any modifications? Do you really get no more debug messages?
Sinan Ünür
sorry here is the full output
tin tin
sorry wont allow me to add it all in one comment so Im breaking it up into two
tin tin
Net::FTP>>> Net::FTP(2.75)Net::FTP>>> Exporter(5.58)Net::FTP>>> Net::Cmd(2.26)Net::FTP>>> IO::Socket::INET(1.27)Net::FTP>>> IO::Socket(1.28)Net::FTP>>> IO::Handle(1.24)Net::FTP=GLOB(0x180c648)<<< 220---------- Welcome to Pure-FTPd [TLS] ----------Net::FTP=GLOB(0x180c648)<<< 220-You are user number 2 of 50 allowed.Net::FTP=GLOB(0x180c648)<<< 220-Local time is now 16:35. Server port: 21.Net::FTP=GLOB(0x180c648)<<< 220-IPv6 connections are also welcome on this server.Net::FTP=GLOB(0x180c648)<<< 220 You will be disconnected after 15 minutes of inactivity.
tin tin
Net::FTP=GLOB(0x180c648)>>> user user_nameNet::FTP=GLOB(0x180c648)<<< 331 User user_name OK. Password requiredNet::FTP=GLOB(0x180c648)>>> PASS ....Net::FTP=GLOB(0x180c648)<<< 230-User user_name has group access to: user_name wheel Net::FTP=GLOB(0x180c648)<<< 230 OK. Current restricted directory is /Net::FTP=GLOB(0x180c648)>>> CWD public_html/uploaded/product_images/Net::FTP=GLOB(0x180c648)<<< 250 OK. Current directory is /public_html/uploaded/product_imagesNet::FTP=GLOB(0x180c648)>>> PORT 192,168,1,10,200,38Net::FTP=GLOB(0x180c648)<<< 200 PORT command successful
tin tin
Net::FTP=GLOB(0x180c648)>>> NLSTNet::FTP=GLOB(0x180c648)<<< 150 Connecting to port 50703Net::FTP=GLOB(0x180c648)<<< 226-Options: -a Net::FTP=GLOB(0x180c648)<<< 226 Output truncated to 2000 matchesNet::FTP=GLOB(0x180c648)>>> ALLO 7903Net::FTP=GLOB(0x180c648)<<< 200 Zzz...Net::FTP=GLOB(0x180c648)>>> PORT 192,168,1,10,200,39Net::FTP=GLOB(0x180c648)<<< 200 PORT command successfulNet::FTP=GLOB(0x180c648)>>> STOR 097360718843.jpegNet::FTP=GLOB(0x180c648)<<< 150 Connecting to port 50704Net::FTP=GLOB(0x180c648)<<< 226-File successfully transferred
tin tin
Net::FTP=GLOB(0x180c648)<<< 226 0.381 seconds (measured here), 20.21 Kbytes per secondNet::FTP=GLOB(0x180c648)>>> SITE chmod 600 097360718843.jpegNet::FTP=GLOB(0x180c648)<<< 200 Permissions changed on 097360718843.jpeg
tin tin
so, it looks the file transfer is happening. Is it possible that it's being written somewhere unexpected? I would also recommend removing that last command just to make sure it's not having any unexpected side-effects.
Mike Ellery
Well, its not actually uploading the file as I cannot see it in the dir. I can create files in this dir and have uploaded files into it too, with a 3rd party ftp program. Im really at a loss as to why this is happening.
tin tin
A lot of these comments should be part of the question.
brian d foy
A: 

Try the code from this blog.

mcandre
sorry wont work, as I have a file that has all the images I want to upload called up.txtit is in the format of tab. for exampleimage.jpegimage2.jpegimage99.jpegect
tin tin
He has code that looks like it should work. He has not looked or provided diagnostics. Therefore, your answer is not very useful.
Sinan Ünür
Sometimes it is easier to start from a working example.
mcandre
+1  A: 

Check the protection on the destination directory. Make sure you can create files there.

Check the protection on the files themselves Make sure you can overwrite them.

EvilTeach
Well, its not actually uploading the file as I cannot see it in the dir. I can create files in this dir and have uploaded files into it too, with a 3rd party ftp program. Im really at a loss as to why this is happening.
tin tin