Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-20-2002, 05:31 PM   PM User | #1
Rich
New to the CF scene

 
Join Date: Jul 2002
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rich is an unknown quantity at this point
Smile Choose file on form and submit ???? Please Help

Hi,

I put a java script in a form which allows the user to choose a file to send with the rest of the form answers. JUST LIKE THE ONE IN THE "POST NEW THREAD" PAGE ON THIS SITE. This is the script below. It browses for the file and it also lets the user view the file too, but I do not receive it in the email with the rest of the info. Should this be a CGI script I am looking for instead of a java script to let the user browse and also submit? Or, maybe it's the wrong script. If not, I can post the rest of the HTML from that page later. Thanks.

Rich


<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Hoop Hooper (hooper119@aol.com) -->
<!-- Web Site: http://www.angelfire.com/pa3/muddas -->
<!-- Modified: Larry McClurg (larry@honeybells.com) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// Thank you Larry McClurg for the cross-browser fix
function whatFile() {
window.location= 'file:///' + document.form1.cmuds.value;
}
// End -->
</script>

</HEAD>

<BODY>

<form name=form1>
<input type=file name="cmuds">
<input type=button onClick="whatFile()" value="Open File">
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.09 KB -->
Rich is offline   Reply With Quote
Old 07-20-2002, 08:54 PM   PM User | #2
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
That's not what you want really if you want to allow users to upload files, that simply allows users to view the files themselves.

Uploading files is definitely a server-side task, and there are plenty of scripts at various free script resources which will be able to help you with this, such as www.hotscripts.com and cgi.resourceindex.com,

Hope that helps..
Mouldy_Goat is offline   Reply With Quote
Old 07-29-2002, 09:24 AM   PM User | #3
crca
New Coder

 
Join Date: Jul 2002
Location: I forgot ;)
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
crca is an unknown quantity at this point
#
# Perl-based Upload Script
#
# Copyright 1998 Tod Sambar
# All rights reserved.
#
# Demonstrates how to upload data via multipart/form-data.
#


#
# PARSE THE CGI FORM
#
$content_type = $ENV{'CONTENT_TYPE'};
$content_len = $ENV{'CONTENT_LENGTH'};
$host_test = $ENV{'REMOTE_ADDR'};
# Buffer the POST content
binmode STDIN;
read(STDIN, $buffer, $content_len);

if ((!$content_type) ||
($content_type eq 'application/x-www-form-urlencoded'))
{
# Process the name=value argument pairs
@args = split(/&/, $buffer);

$data = '';
foreach $pair (@args)
{
($name, $value) = split(/=/, $pair);

# Unescape the argument value
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# Save the name=value pair for use below.
$FORM{$name} = $value;
}
}
elsif ($content_type =~ m#^multipart/form-data#)
{
# find boundary
# Eric Poulsen fixed the following to allow for quotes.
#
# ($boundary = $content_type) =~ s/^.*boundary=(.*)$/\1/;
($boundary = $content_type) =~ s/^.*boundary="?(.*?)"?$/\1/;

@pairs = split(/--$boundary/, $buffer);
@pairs = splice(@pairs,1,$#pairs-1);

for $part (@pairs)
{
($dump,$fline,$value) = split(/\r\n/,$part,3);
next if $fline =~ /filename=\"\"/;
$fline =~ s/^Content-Disposition: form-data; //;
(@columns) = split(/;\s+/, $fline);
($name = $columns[0]) =~ s/^name="([^"]+)"$/\1/g;

if ($#columns > 0)
{
if ($value =~ /^Content-Type:/)
{
($dump,$dump,$value) = split(/\r\n/,$value,3);
}
else
{
($dump,$value) = split(/\r\n/,$value,2);
}
}
else
{
($dump,$value) = split(/\r\n/,$value,2);
if (grep(/^$name$/, keys(%CGI)))
{
if (@{$FORM{$name}} > 0)
{
push(@{$FORM{$name}}, $value);
}
else
{
$arrvalue = $FORM{$name};
undef $FORM{$name};
$FORM{$name}[0] = $arrvalue;
push(@{$FORM{$name}}, $value);
}
}
else
{
next if $value =~ /^\s*$/;
$FORM{$name} = $value;
}
next;
}

$FORM{$name} = $value;
}
}
else
{
print "Invalid content type!\n";
exit(1);
}

#
# VERIFY THE FORM DATA
#
$upfile = $FORM{'upfile'};
$upname = $FORM{'upname'};
if (!($upfile) || !($upname))
{
print "<HTML><TITLE>Missing fields</TITLE><BODY>\n";
print "No upload file specified!\n";
print "</BODY></HTML>\n";
exit(1);
}


#
# CLOSE SECURITY PROBLEMS.
#
if ($upname =~ /[;><&\*'\|\/\\]/ )
{
print "<HTML><TITLE>Invalid file name</TITLE><BODY>\n";
print "The upload file name is invalid.\n";
print "</BODY></HTML>\n";
exit(1);
}

#
# Write out the upload file
#
$filename = "../docs/upload/".$upname;
open(FILE, ">$filename") || exit(1);
binmode FILE;

print FILE $upfile;
close FILE;


#
# DONE
#
print "Upload of ".$upname." succeeded.\n";

exit(0);
__________________
PM Me if you want the new URL for my forum!
crca is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 PM.


Advertisement
Log in to turn off these ads.