Moving files to server

Hi there!

I don't know perl at all and would highly appreciate your help. Here's what I need:

I've got some *.jpg/*.gif file in my local directory (c:\...) and I need to move it as it is to the following server directory:

<base path>/<id>/,

while <base path>  exists, but <id> may not exist.

Can *.pl script solve this? I must run it from Java.

Thanks a lot.
kalle73Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
thoellriConnect With a Mentor Commented:
This sounds a lot different than your original question - do you actually know what you want?

Anyway, here are the changes: If you would save this script as "transfer.pl" you would call it with: "transfer.pl /path/to/local/file.xyz /destination/directory/on/server"
If either of the args are missing then the script will die.

Hope *this* helps
  Tobias


use Net::FTP;

$local_file=$ARGV[1] || die "need local file as first argument";
$destination=$ARGV[2] || die "need destination directory";

# adjust the following variables:
# what is the hostname of the server?
$server="someserver.somecompany.com";
# what is the username of the user who puts the files up there?
$server_user="username";
# what is this users password?
$server_password="password";

# open ftp connection to server
$ftp = Net::FTP->new($server) || die "Can't connect to $server -$!";
# login to server
$ftp->login($server_user,$server_password);
# make the destination directory
                    $ftp->mkdir("$destination",1);
# make the new directory the current directory
$ftp->cwd("$destination");
# transfer files in binary mode
$ftp->binary();
# store local file to server
$ftp->put("$local_file");
# close ftp connection
$ftp->close();
0
 
ozoCommented:
mkdir "$basepath/$id",0755 or die "can't mkdir $basepath/$id $!";
0
 
guadalupeCommented:
Use Net::FTP

I'll try and send more detailed info in a second...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
thoellriCommented:
Something like this should work. I assume the server is a Unix box? Or a system which supports FTP?

 Hope this helps
   Tobias


use Net::FTP;

# adjust the following variables:

# where are the local *.jpg/*.gif files?
$local_directory="c:\\";
# what is the hostname of the server?
$server="someserver.somecompany.com";
# what is the username of the user who puts the files up there?
$server_user="username";
# what is this users password?
$server_password="password";
# what is the basepath?
$server_basepath="/some/path";
# what is this "special" id?
$server_id="someid";

# open ftp connection to server
$ftp = Net::FTP->new($server) || die "Can't connect to $server -$!";
# login to server
$ftp->login($server_user,$server_password);
# make the basedir/id directory
$ftp->mkdir("$server_basepath/$server_id",1);
# make the new directory the current directory
$ftp->cwd("$server_basepath/$server_id");
# transfer files in binary mode
$ftp->binary();
# read *.gif/*.jpg filenames
opendir(DIR,$local_directory) || die "can't read dir $local_directory $!";
@files=grep { /\.(gif|jpg)$/i && -f "$local_directory/$_" } readdir(DIR);
closedir(DIR);
# put files on server
foreach (@files) {
  $ftp->put("$local_directory/$_",$_);
}
# close ftp connection
$ftp->close();
0
 
kalle73Author Commented:
thoellri

Thanks, I'll check whether it works. If it is, you'll get your points.
0
 
kalle73Author Commented:
thoellri

Actually I need a script which runs on a server. That means that I call its URL (from Java) with params: my local file and destination directory on a server and it moves it.
0
 
kalle73Author Commented:
Thanks, it worked.
0
All Courses

From novice to tech pro — start learning today.