Link to home
Start Free TrialLog in
Avatar of kalle73
kalle73

asked on

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.
Avatar of ozo
ozo
Flag of United States of America image

mkdir "$basepath/$id",0755 or die "can't mkdir $basepath/$id $!";
Avatar of guadalupe
guadalupe

Use Net::FTP

I'll try and send more detailed info in a second...
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();
Avatar of kalle73

ASKER

thoellri

Thanks, I'll check whether it works. If it is, you'll get your points.
Avatar of kalle73

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of thoellri
thoellri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kalle73

ASKER

Thanks, it worked.