Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

How to upload file without using FTP?

by madtoperl (Pilgrim)
on May 24, 2006 at 08:50 UTC ( #551303=perlquestion: print w/replies, xml ) Need Help??
madtoperl has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks!!!

I am trying to implement a script which upload a fixed gif and html files from a fixed local directory into a fixed folder of our
website, which will be happened everyday, so that i am trying to do some automated process so that I don't have to use
FTP software to upload that file everyday. I have tried the following methode, its uploading the file from local machine to
the web directory through a form, but the problem is it can't read the file size, so its copying 0 bite size file. (but if chose
the file from this Form and say upload its working fine, but if I supply the local directiry path its not working) the code as below
my $file_query1 = 'D:\work for people\k****ij\oil1.shtml'; sub Upload { my($query, $upload_dir) = @_; my($file_query, $file_name, $size, $buff, $time, $bytes_count); $size = $bytes_count =0; $_ = $file_query1; s/\w://; s/([^\/\]+)$//; $_ = $1; s/\.\.+//g; s/\s+//g; $file_name = $_; open(FILE,">$upload_dir/$file_name") || &Error("Error opening file $fi +le_name for writing, error $!", 1); binmode FILE; $time=time(); while ($bytes_count = read($file_query,$buff,2096)) { $size += $bytes_count; print FILE $buff; } close(FILE); print "completed<br><br>"; }

In the code this following portion is not working:
---------------------------------------------------------------------- +--------------- open(FILE,">$upload_dir/$file_name") || &Error("Error opening file $fi +le_name for writing, error $!", 1); binmode FILE; $time=time(); while ($bytes_count = read($file_query,$buff,2096)) { $size += $bytes_count; print FILE $buff; } close(FILE);
I simply want to use a CGI or perl script to FTP or transfer some perticular file with from a fixed local folder to a fixed
webfolder, but without usiing manual FTP client software.
can anyone help me with this perticular matter? if you need any more info please let me know. if you think is there any
other way please let me know..

2006-10-08 Unapproved by planetscape once evidence of habitual plagiarism uncovered; Keep: 3, Edit: 10, Reap: 16

Replies are listed 'Best First'.
Re: How to upload file without using FTP?
by McDarren (Abbot) on May 24, 2006 at 08:59 UTC
    No offense intended, but that code of yours is pretty ugly. And you're un-necessarily trying to re-invent a wheel that already exists and works perfectly.

    Personally, I would be using Net:SCP. Or alternatively, you might want to use Net:FTP


Re: How to upload file without using FTP?
by arkturuz (Curate) on May 24, 2006 at 09:12 UTC
      Want to skip Your computer and transfer from ftp to ftp ? Here You can transfer files between two FTP servers on the Internet Directransfer supports transferring entire directory structures while automatically determining whether each file should be transferred in text or binary mode. Directransfer save your time. What do you basically need to start upload? You need something to upload and you need to know server name and your login to this server. That's all . You just point to something you want to upload in your explorer, right-click and choose destination. Program just silently does the task without bothering you again. Quite handy. Supports secure authentication (MD5).
Re: How to upload file without using FTP?
by zentara (Archbishop) on May 24, 2006 at 10:44 UTC
    You can use many different things to upload it... lwp, wget, curl, or even pure sockets. Here is an lwp based uploader with a Tk front-end -> tk-http-file-upload-w-progress You can rip out the Tk code if you want, and have a console app

    Here is the cgi which receives it.

    #!/usr/bin/perl use warnings; use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; #my $maxsize = 1024 * 100; #max 100K my $maxsize = 1024 * 20000; #max 20M #$CGI::POST_MAX= $maxsize; # max 100K posts !not working right? #$CGI::DISABLE_UPLOADS = 1; # no uploads my $query = new CGI; my $upload_dir = "uploads"; #permissions for dir are set print $query->header(); if($ENV{CONTENT_LENGTH} > $maxsize){ print "file too large - must be less than $maxsize bytes"; exit; } my $file = $query->param("file"); my $filename = $file; $filename =~s/.*[\/\\](.*)/$1/; open (UPLOADFILE, ">$upload_dir/$filename"); $/= \8192; # sets 8192 byte buffer chunks, perldoc perlvar while ( <$file> ){ print UPLOADFILE $_; #select(undef,undef,undef,.05); #for testing } close UPLOADFILE; print <<END_HTML; <HTML> <HEAD> <TITLE>Thanks!</TITLE> </HEAD> <BODY bgcolor="#ffffff"><br> <P>Thanks for uploading file : $filename!</P> </BODY> </HTML> END_HTML

    I'm not really a human, but I play one on earth. flash japh
Re: How to upload file without using FTP?
by roboticus (Chancellor) on May 24, 2006 at 11:39 UTC

    A couple of things: First, in the code you've shown, you've forgotten to set $upload_dir, so your open may be failing because of that. Secondly, your regular expression is missing a character. I think you meant this:

    But after seeing what you're doing, I'd simplify and rearrange the filename parsing bit to look like this:

    my ($drive, $path, $file); die "Invalid file name!" unless $file_query1 =~ /(\w:)?(.+)[\\\/](.*)/; print "Drive=", $1, ", Path=", $2, ", File=", $3, "\n";
    In any case, I'd second McDarren's suggestion. I've used Net::FTP and it works nicely for jobs like this.


Re: How to upload file without using FTP?
by TedPride (Priest) on May 24, 2006 at 17:18 UTC
    Net::FTP (which comes with the standard Perl installation) will make this a whole lot simpler. Just install Perl on your comp, and use something like the following:
    use strict; use warnings; use Net::FTP; my ($ftp, $host, $user, $pass, $dir, $fpath); $host = ""; $user = "myuser"; $pass = "mypass"; $dir = "/www/htdocs"; $fpath = "D:\work for people\k****ij\oil1.shtml"; $ftp = Net::FTP->new($host, Debug => 0); $ftp->login($user, $pass) || die "Bad login"; $ftp->cwd($dir) || die "Unable to change directories"; $ftp->put($fpath) || die "Unable to upload file"; $ftp->quit; print "Success!";
    Then if you don't want to have to run even this manually, set up an automatic job to call perl every so often.
Re: How to upload file without using FTP?
by hossman (Prior) on Oct 08, 2006 at 20:53 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://551303]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2017-02-23 23:59 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (351 votes). Check out past polls.