Listing of upload.pl


#!/usr/bin/perl -w #------------------------------------------------------------------------------ # upload: move the files associated with current project #------------------------------------------------------------------------------ use strict; use Net::FTP; use File::Spec; sub fileType($); sub transfer($); #-------------------------------------------------------------------------- # make the ftp connection first # Note: change "ftp site", "username", and "password" to the real thing #-------------------------------------------------------------------------- my $os = $^O; my $ftp = Net::FTP->new("ftp site", Passive => 1) or die "Can't connect.\n"; $ftp->login("username", "password") or die "Couldn't login.\n"; $ftp->cwd("public_html/" . $ARGV[1]) or die "Couldn't cwd $ARGV[1]\n"; #-------------------------------------------------------------------------- # if single file, just do that. Otherwise, loop through directory #-------------------------------------------------------------------------- my $file; if (fileType($ARGV[0]) eq "directory") { print "Transferring directory: $ARGV[0] to $ARGV[1]...\n"; my $dir = File::Spec->catfile($ARGV[0], "*.*"); my @list = glob($dir); foreach $file (@list) { transfer($file); } } else { print "Transferring file: $ARGV[0] to $ARGV[1]...\n"; transfer($ARGV[0]); } $ftp->quit(); exit(0); #-------------------------------------------------------------------------- # transfer file #-------------------------------------------------------------------------- sub transfer($) { my ($to, $volume, $directories, $attr); my ($from) = @_; ($volume, $directories, $to) = File::Spec->splitpath($from); $attr = fileType($from); #-------------------------------------------------------------------------- # choose transfer type depending on file type and operating system # Under windows, I found I needed to transfer everything in binary #-------------------------------------------------------------------------- if ($os eq "MSWin32") { $ftp->binary; } elsif ($attr eq "text" || $attr eq "perl") { $ftp->ascii; } else { $ftp->binary; } print " " . $to . " (" . $attr . ")\n"; $ftp->put($from, $to) or die("Couldn't send $to: $ftp->message\n"); #-------------------------------------------------------------------------- # if perl file, make it executable (fix later) #-------------------------------------------------------------------------- if ($attr eq "perl") { # $ftp->chmod(755, $to) or die $ftp->message; } } #-------------------------------------------------------------------------- # system specific test for file type # returns: text, perl, binary, directory #-------------------------------------------------------------------------- sub fileType($) { my $test = lc($_[0]); #-------------------------------------------------------------------------- # Windows #-------------------------------------------------------------------------- if ($os eq "MSWin32") { if (! ($test =~ /\./)) { return "directory"; } if ($test =~ /.pl$/) { return "perl"; } if ($test =~ /.txt$|.html$|.xml$|.php$/ ) { return "text"; } return "binary"; } #-------------------------------------------------------------------------- # unix #-------------------------------------------------------------------------- if ($test =~ /.pl$/) { return "perl"; } my $type =`file -b $test`; if ($type =~ /directory/) { return "directory"; } elsif ($type =~ /text/) { return "text"; } else { return "binary"; } }