> On Jan 12, 2008 5:54 PM, Patrik Hasibuan <[EMAIL PROTECTED]>
> wrote:
> I am still new in perl.
On Jan 12, 2008 9:33 PM, Sean Davis <[EMAIL PROTECTED]> wrote:
> Hi, Patrik.
>
> You should probably be using CGI.pm.  It will vastly simplify what you are
> doing.  Then, you can read any of a dozen sites on doing file uploads using
> CGI; see search.cpan.org for the details of using CGI.pm.
>
> Sean

I agree with Sean.  You have 'use CGI' in your code, but you don't
actually use it, you really should.

> >
> > I am writing perl-cgi application for uploading a file. I did "chmod
> > 777 ../../artikel". But I get weird displayed message:
> > "
> > ueri: ------------4CyrMz2ZeGIClwYfFsVdcv Co........
> > î 6êÎ]Ë kšfþx·¾ ðfS4M3>º {½‡<Óöù³(R)�¯3çýGèBù= „¬È›øRƒ.....
> > &ƒ ÿƒ&m‡îø'-n nÊÐJ(pÇ 9çqîÎ.........
> > Ê ¨‚¡ÀÜ EÖ� z!ª˜Y6�¬úÒ„â0J¼"ñË.......
> > [end so on. very longg.............]
> > ".
> >
This line in the upload cgi was where all the output was coming from:
> print "kueri: $kueri<br>";

The output you were seeing was all of the variables in the form,
including the contents of the uploaded file.

You should always enable warnings and use strict in CGI code.  use
strict will alert you to many errors, like misspelled variable names,
that can be time consuming to track down.  use strict also forces you
to declare variables before using them which is why there are lots of
my $var ... statements in the hacked up code.

I hacked up your code and got it to work, except for the upload bit,
which was easier to rewrite using CGI than to fix.

#!/usr/bin/perl -w
# deartikel.cgi
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

print "Content-type: text/html\n\n";
print "<html><head></head>";
print "<body><font style=\"Arial,Arial Black,Helvetica\" size=1>";
print " <form method=\"post\" action=\"cgideartikel.cgi\"> ";
print "Title of file <input type=\"text\" name=\"judul\" size=60
autocomplete=\"ON\"> ";
print "<input type=\"submit\" value=\"Submit to column 'Articles'.\">"
     . "<input type=\"reset\" value=\"Input lagi?\">";
print " </form></body></html>";

#!/usr/bin/perl -w
# cgideartikel.cgi:
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI;

print "Content-type: text/html\n\n";
print "<html><head><title>Online Science Org - Data entry kolom
karir.</title></head>"
    . "<body><font style=\"Arial,Arial Black,Helvetica\" size=1>";
my $pjg = $ENV{"CONTENT_LENGTH"};
my $kueri;
read(STDIN, $kueri, $pjg);

my @arr_prm = split("&",$kueri);
my (%assprm, $namavar, $nilaivar);
foreach my $myprm(@arr_prm){
 ($namavar,$nilaivar) = split("=",$myprm);
 $assprm{$namavar} = $nilaivar;
}
my $judul = $assprm{'judul'};
print " <form method=\"post\" action=\"cgiupload.cgi\"
enctype=\"multipart/form-data\"> ";
print "Judul<input type=\"text\" name=\"judul\" size=60 autocomplete=\"ON\"
value=\"$judul\" readonly> ";
print " <input type=\"file\" name=\"url\" size=60 autocomplete=\"ON\"> ";
print "<input type=\"submit\" value=\"Submit to column 'Artikel'.\">
       <input type=\"reset\" value=\"Input lagi?\">";
print " </form></body></html>";
##mw BUG &simpan(judul);
&simpan($judul);

sub simpan{
my $myjudul = shift;
my $box = "localhost";
my $basisdata = "mydb";
my $db_user_name = 'root';
my $db_password = 'mypw';
my $dsn = "DBI:mysql:$basisdata:$box";

my $dbh = DBI->connect($dsn, $db_user_name, $db_password)
        or die "Unable to connect to $dsn $db_user_name $DBI::errstr";
my $sqlstr = qq{ insert into artikel ( judul) values( '$myjudul') };
my $sth = $dbh->prepare($sqlstr) or die $dbh->errstr;
my $status = $sth->execute() or die $dbh->errstr;
if($status){
#mw BUG       $sth->{Commit};
        $dbh->commit(); # also, on my server autocommit is enabled, so
this emit an error to the log
# see: perldoc DBI for more info on this...
}else{
#mw BUG       $sth->{RollBack};
        $dbh->rollback();
}
$dbh->disconnect;
}

#!/usr/bin/perl
# cgiupload.cgi
use warnings;
use strict;
use CGI;

my $cgi = CGI->new();
print $cgi->header('text/html');
print $cgi->start_html(-title=>'Article upload');
my $filename = $cgi->param('url');
print $cgi->p("filename => ($filename)");
my $OUTFILE;
my $path = '/var/www/cgi-bin/test';
open ($OUTFILE,">$path/$filename") or die "Could not open output file
$path/$filename $!";
my ($bytesread, $buffer);
while ($bytesread=read($filename,$buffer,1024)) {
    print $OUTFILE $buffer;
}
close $OUTFILE;
print $cgi->h1('Article upload status');
print $cgi->p('article uploaded');
print $cgi->end_html();

Notice that the upload code using CGI is about one third the length of
your non-working code.
There should be more error checking added, but it is up and limping.

You should look at the perldoc for CGI.   In particular search for MAX
(with /MAX) and read the section about settint the maximum size of
uploads.

Cheers,

Mike

Reply via email to