Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Image upload

by Anonymous Monk
on May 02, 2002 at 15:49 UTC ( [id://163561]=perlquestion: print w/replies, xml ) Need Help??

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

Hey, I'm fairly new to Perl, and I have a project I would like to create. I want to be able to upload an image from my browser and have the script FTP the image to one of five different directories on another server, depending on which category is selected in a drop down box. I'm not sure how to get the script to upload an image, but I think once it is uploaded I can use an If Else statement to determine which directory to send to. Does anyone have any ideas? Thanks!

Replies are listed 'Best First'.
Re: Image upload
by adamcrussell (Hermit) on May 02, 2002 at 16:41 UTC
    If/Else is probably fine for this. You may not want to use it is you anticpate the number of individual project directories to grow very much though. This is because you want have a big long ugly list of if/elses. What I would do is to store the project dirs in a hash keyed by project title. Your form would submit this key and you could then set the directory by pulling the value from the hash. Your hash would look like this:
    %projectdirs = ( "hypnotron", "/export0/hypnotron", "death_ray", "/export0/death_ray" );
    Retrieving from the hash is just this:
    $directory=$projectdirs{$key}
    Hope that helps. Good luck.
Re: Image upload
by beebware (Pilgrim) on May 03, 2002 at 22:00 UTC
    You might find this subroutine that I wrote of us. It's currently being run as a cronjob on one of our servers and just transfers files via FTP to another site...
    use Net::FTP; $screen=1; # change to 0 for 'quiet' operation (ie no screen display) sub move_files { # moves a file by FTP to a remote site # Code by Richard Chiswell # http://www.beebware.com/ # Free usage, acknowledgements would be appreciated tho! my ($destination,$file)=@_; # destination is in the format username:password@ftp.example.com # file is the local full path name of the file to be transfered my ($username,$password,$host,$path,$ftp); my ($results,$logfile,$movedlog); # first let's split the destination information apart $destination=~/^([^\:]+)\:([^\@]+)\@([^\/]+)\/(.*)/; ($username,$password,$host,$path)=($1,$2,$3,$4); $path="/".$path; print "Destination:$destination\n" if $screen; print "Login as $username (password: $password) onto:\n" if $screen; print " Host: $host\n" if $screen; print " Path: $path\n" if $screen; $logfile.="Login as $username (password: $password) onto:\n"; $logfile.=" Host: $host\n Path: $path\n"; $logfile.=" Time: ".&show_time(time())."\n\n"; if (length($host)>1) { $ftp=Net::FTP->new($host,Debug=>0); if (!defined($ftp)) { print "* Unable to connect to FTP server:\n $@\n" if $screen; $logfile.= "*********************************************\n"; $logfile.="* UNABLE TO CONNECT TO FTPSERVER! *\n"; $logfile.="$@\n"; } else { $logfile.="Connected\n"; $results=$ftp->login($username,$password); if (!$results) { print "* Unable to login to FTP server:\n" if $screen; print $ftp->message()."\n" if $screen; $logfile.="********************************************\n"; $logfile.="* UNABLE TO LOGIN TO FTP SERVER! *\n"; $logfile.=$ftp->message()."\n"; } else { print "Logged in\n" if $screen; $logfile.="Successfully logged in\n"; $ftp->cwd($path); print "Changed to $path\n ".$ftp->message()."\n" if $screen; print "Current path is ".$ftp->pwd()."\n" if $screen; $logfile.="Path: ".$ftp->pwd()."\n"; $ftp->hash(1024); $ftp->binary(); $results=$ftp->put($file); if (!$results) { print "* Unable to transfer: file\n" if $screen; print $ftp->message()."\n" if $screen; $logfile.="* Unable to transfer:$file\n"; $logfile.=" Message:".$ftp->message()."\n"; } else { $movedlog.="Moved $file\n";unlink $file; $logfile.="Moved $file\n"; } $ftp->quit; } } $movedlog="These files have been moved to\n"; $movedlog.="$destination\n\n$movedlog\n"; } else { print "Unable to read FTP details!\n" if $screen; } return "$movedlog\n\n*****************************\n\n$logfile"; print "DONE!\n" if $screen; }
    So for example, to transfer the file /temp/file.txt to ftp.example.com with the login username of 'user' and the password 'pass', just call $log=&move_files("user:pass@ftp.example.com","/temp/file.txt");. The $log can then be either emailed (as we have set it up) or just stored in a log file or disgarded. I hope it helps.
Re: Image upload
by silent11 (Vicar) on May 03, 2002 at 15:06 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://163561]
Approved by jlk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2025-03-22 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.