PDA

View Full Version : Problem with my file upload script


dustywb
11-21-2004, 12:43 AM
damn thing wont work could anyone help me out with it?

this is the html file
<HTML>
<HEAD></HEAD>
<BODY>
<FORM ACTION="/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data">
Photo to Upload: <INPUT TYPE="file" NAME="photo">
<br><br>
Your Email Address: <INPUT TYPE="text" NAME="email_address">
<br><br>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
</FORM>
</BODY>
</HTML>

and it points to this cgi file

#!/usr/bin/perl -w

use CGI;

$upload_dir = "/home/localgol/public_html/pics";

$query = new CGI;

$filename = $query->param("photo");
$email_address = $query->param("email_address");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("photo");

open UPLOADFILE, ">$upload_dir/$filename";

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;

print $query->header ( );
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your photo!</P>
<P>Your email address: $email_address</P>
<P>Your photo:</P>
<img src="/upload/$filename" border="0">

</BODY>
</HTML>

END_HTML

any help would be greatly appreciated

mlseim
11-21-2004, 01:08 AM
What is not working?
Are you getting a program script failure,
or are your images just not uploading?

You have everything CHMOD'd OK?
The directory you're uploading your images into
CHMOD = 777?

dustywb
11-21-2004, 01:17 AM
getting a 500 server error and the tutorial I did for this said chmod=755 which is what it is

dustywb
11-21-2004, 01:25 AM
I changed permissions to 777 still same error

mlseim
11-21-2004, 02:37 AM
#!/usr/bin/perl

$upload_dir = "/home/localgol/public_html/pics";

@fname=();
binmode(STDIN);
&Parse_Multi;

foreach $item (keys %CGI) {
$var="$item";
$val="$CGI{$item}";
if ($var =~ /\_val/is) {
if ($val eq "") {$var=~ s#\_val##is;$var=~ s#^(.*?)\_##is;error("$var cannot be empty!");}
}
}

$fl=$CGI{"photo"}->{'Contents'};
$filename=$CGI{"photo"}->{'filename'};
open(TMP, ">$upload_dir/$filename") || error("Cannot upload file $filename: $!");

print TMP "$fl";
close TMP;
push @fname, "$filename";

print "Content-Type: text/html\n\n";
print qq~
<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your photo!</P>
<P>Your email address: $email_address</P>
<P>Your photo:</P>
<img src="$upload_dir/$filename" border="0">

</BODY>
</HTML>

~;

sub Parse_Multi {
local($boundary,@pairs,$position);
local($raw_data,$value,$name,$part);

$raw_data = &Parse_Method;
($boundary = $ENV{CONTENT_TYPE}) =~ s/^.*boundary=(.*)$/\1/;
@pairs = split(/--$boundary/, $raw_data);
@pairs = splice(@pairs,1,$#pairs-1);

for $part (@pairs) {
$part =~ s/[\r]\n$//g;
($dump, $firstline, $datas) = split(/[\r]\n/, $part, 3);
next if $firstline =~ /filename=\"\"/;
$firstline =~ s/^Content-Disposition: form-data; //;
(@columns) = split(/;\s+/, $firstline);
($name = $columns[0]) =~ s/^name="([^"]+)"$/\1/g;
if ($#columns > 0) {
if ($datas =~ /^Content-Type:/) {
($CGI{"$name"}->{'Content-Type'}, $blankline, $datas) = split(/[\r]\n/, $datas, 3);
$CGI{"$name"}->{'Content-Type'} =~ s/^Content-Type: ([^\s]+)$/\1/g;
}
else {
($blankline, $datas) = split(/[\r]\n/, $datas, 2);
$CGI{"$name"}->{'Content-Type'} = "application/octet-stream";
}
}
else {
($blankline, $datas) = split(/[\r]\n/, $datas, 2);
if (grep(/^$name$/, keys(%CGI))) {
if (@{$CGI{$name}} > 0) {
push(@{$CGI{$name}}, $datas);
}
else {
$arrvalue = $CGI{$name};
undef $CGI{$name};
$CGI{$name}[0] = $arrvalue;
push(@{$CGI{$name}}, $datas);
}
}
else {
next if $datas =~ /^\s*$/;
$CGI{"$name"} = $datas;
}
next;
}
for $currentColumn (@columns) {
($currentHeader, $currentValue) = $currentColumn =~ /^([^=]+)="([^"]+)"$/;
$CGI{"$name"}->{"$currentHeader"} = $currentValue;
}
$CGI{"$name"}->{'Contents'} = $datas;
}
}
sub Parse_Method {
local($buffer);

if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
return $buffer;
}
elsif ($ENV{'REQUEST_METHOD'} eq "GET") {
return $ENV{'QUERY_STRING'};
}
else {
return 0;
}
}

dustywb
11-21-2004, 02:57 AM
#!/usr/bin/perl

$upload_dir = "/home/localgol/public_html/pics";

@fname=();
binmode(STDIN);
&Parse_Multi;

foreach $item (keys %CGI) {
$var="$item";
$val="$CGI{$item}";
if ($var =~ /\_val/is) {
if ($val eq "") {$var=~ s#\_val##is;$var=~ s#^(.*?)\_##is;error("$var cannot be empty!");}
}
}

$fl=$CGI{"photo"}->{'Contents'};
$filename=$CGI{"photo"}->{'filename'};
open(TMP, ">$upload_dir/$filename") || error("Cannot upload file $filename: $!");

print TMP "$fl";
close TMP;
push @fname, "$filename";

print "Content-Type: text/html\n\n";
print qq~
<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your photo!</P>
<P>Your email address: $email_address</P>
<P>Your photo:</P>
<img src="$upload_dir/$filename" border="0">

</BODY>
</HTML>

~;

sub Parse_Multi {
local($boundary,@pairs,$position);
local($raw_data,$value,$name,$part);

$raw_data = &Parse_Method;
($boundary = $ENV{CONTENT_TYPE}) =~ s/^.*boundary=(.*)$/\1/;
@pairs = split(/--$boundary/, $raw_data);
@pairs = splice(@pairs,1,$#pairs-1);

for $part (@pairs) {
$part =~ s/[\r]\n$//g;
($dump, $firstline, $datas) = split(/[\r]\n/, $part, 3);
next if $firstline =~ /filename=\"\"/;
$firstline =~ s/^Content-Disposition: form-data; //;
(@columns) = split(/;\s+/, $firstline);
($name = $columns[0]) =~ s/^name="([^"]+)"$/\1/g;
if ($#columns > 0) {
if ($datas =~ /^Content-Type:/) {
($CGI{"$name"}->{'Content-Type'}, $blankline, $datas) = split(/[\r]\n/, $datas, 3);
$CGI{"$name"}->{'Content-Type'} =~ s/^Content-Type: ([^\s]+)$/\1/g;
}
else {
($blankline, $datas) = split(/[\r]\n/, $datas, 2);
$CGI{"$name"}->{'Content-Type'} = "application/octet-stream";
}
}
else {
($blankline, $datas) = split(/[\r]\n/, $datas, 2);
if (grep(/^$name$/, keys(%CGI))) {
if (@{$CGI{$name}} > 0) {
push(@{$CGI{$name}}, $datas);
}
else {
$arrvalue = $CGI{$name};
undef $CGI{$name};
$CGI{$name}[0] = $arrvalue;
push(@{$CGI{$name}}, $datas);
}
}
else {
next if $datas =~ /^\s*$/;
$CGI{"$name"} = $datas;
}
next;
}
for $currentColumn (@columns) {
($currentHeader, $currentValue) = $currentColumn =~ /^([^=]+)="([^"]+)"$/;
$CGI{"$name"}->{"$currentHeader"} = $currentValue;
}
$CGI{"$name"}->{'Contents'} = $datas;
}
}
sub Parse_Method {
local($buffer);

if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
return $buffer;
}
elsif ($ENV{'REQUEST_METHOD'} eq "GET") {
return $ENV{'QUERY_STRING'};
}
else {
return 0;
}
}

I changed the code to this and still server 500 error :(

YUPAPA
11-21-2004, 03:55 AM
#!/usr/bin/perl
use CGI qw(:standard);

my $cgi = new CGI;
my $upload_dir = './uploads';

my $file = $cgi->param('file');
my $filename = $file;
$filename =~ s/^.*(\\|\/)//g;

print $cgi->header(-type=>'text/html');

open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!";
while(<$file>) {
print OUT;
}
close(OUT);

print "$filename has uploaded\n";

__END__



<HTML>
<HEAD></HEAD>
<BODY>
<FORM ACTION="/cgi-bin/upload.cgi" METHOD="post" ENCTYPE="multipart/form-data">
Photo to Upload: <INPUT TYPE="file" NAME="file">
<br><br>
Your Email Address: <INPUT TYPE="text" NAME="email_address">
<br><br>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
</FORM>
</BODY>
</HTML>


NOTICE:
<INPUT TYPE="file" NAME="file">

if you want NAME="photo", then please edit the script to $cgi->param('photo'); instead of $cgi->param('file'); on line 7

mlseim
11-21-2004, 04:09 AM
YUPAPA's solution much shorter than mine ... :o

But ... anyhow, I actually tried mine out and it works OK, using
your HTML form and the script I provided.

dustywb, you must be doing something wrong with your installation
or CHMOD, or something ... maybe your path is wrong?
Are you uploading your script in ASCII form (not Binary)?

The script works just fine for me.

dustywb
11-21-2004, 09:45 AM
YUPAPA's solution much shorter than mine ... :o

But ... anyhow, I actually tried mine out and it works OK, using
your HTML form and the script I provided.

dustywb, you must be doing something wrong with your installation
or CHMOD, or something ... maybe your path is wrong?
Are you uploading your script in ASCII form (not Binary)?

The script works just fine for me.

uhh possibly a stupid question but as I said before I am new to this, how do I make it binary?

mlseim
11-21-2004, 04:10 PM
When you upload your CGI (Perl) scripts to your webspace using
an FTP program (is that how you're uploading the scripts?)

With the FTP program, there's a checkbox somewhere that selects the
transfer process as either Binary or ASCII. With HTML, graphics, etc,
the transfer process gets selected to Binary (usually the default).
With Perl scripts that get uploaded to your CGI-BIN directory, that
setting. or checkbox, (whatever it is with your FTP) must be set to ASCII mode.

dustywb
11-21-2004, 11:04 PM
ok, I tried your script mlseim, I uploaded in ascii and I set the permissions at 777, the directory is set at the same and still server 500 error

mlseim
11-21-2004, 11:50 PM
Do you have any other Perl scripts that run without any errors?

Like a simple "hello world" test script?

That would make sure your CGI-BIN is operating correctly.

#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "Hello World.\n";

dustywb
11-21-2004, 11:59 PM
well hello world dont work so I guess it's the cgi-bin I'll send a msg to the hosting company, thanks for all your help

dustywb
11-22-2004, 02:30 AM
Just wanted to let everyone know (if you care, I dunno :) anyways the hosting company fixed it and it all works wonderfully thanks for the help though, this is the best site for help I've ever been to thanks everyone for the help.

mlseim
11-22-2004, 02:50 AM
whew :D

Glad you got that straightened out. That's the thing with Perl,
sometimes you bang your head for so long.

Anyhow, Good luck coding and come back for help again.
Also, when stuck on something and need immediate help,
go to Google and search for help using "Perl" as the first
word .... example: perl array tutorial

Tons of Perl examples and tutorials on the web.

--max--