Gossamer Forum
Home : Products : Links 2.0 : Customization :

Re: Here's a working image upload mod!

Quote Reply
Re: Here's a working image upload mod! In reply to
I am trying to get it to work on add.cgi, but I get Unknow tag Title when I try to upload an image. Everything works fine when I don't try to upload an image. here's the code for site html add form:

sub site_html_add_form {
# --------------------------------------------------------
# This routine determines how the add form page will look like.
#
&html_print_headers;
my $category = shift;
$category ?
($category = qq~$category <input type=hidden name="Category" value="$category">~) :
($category = &build_select_field ("Category", "$in{'Category'}"));

print &load_template ('add.html', {
Category => $category,
Key => $Key,
Receivemail => $Receivemail,
street_address_url => &encode_map_url ($rec{'Street Address url'}),
city_url => &encode_map_url ($rec{'City url'}),
state => &encode_map_url ($rec{'State'}),
zip => ($rec{'ZIP'}),
phone_number => ($rec{'Phone Number'}),
Image_1 => $image_1,
Caption_1 => $caption_1,
Image_2 => $image_2,
Caption_2 => $caption_2,
%in,
%globals
});
}

here is the add.html code

<form action="<%db_cgi_url%>/add.cgi" method="POST">
<p>Please fill out the form completely, and we'll add your resource as soon as possible.</p>
<div class="margin">
<table border ="0" cellspacing="0" cellpadding="0">
<tr><td align="right" valign="top">Title:</td>
<td><input name="Title" size="50"></td></tr>
<tr><td align="right" valign="top">URL:</td>
<td><input name="URL" size="50"></td></tr>
<tr><td align="right" valign="top">Street Address (if applicable):</td>
<td><input name="Street Address url" value="" size="40"></td></tr>
<tr><td align="right" valign="top">City (if applicable):</td>
<td><input name="City url" value="" size="40"></td></tr>
<tr><td align="right" valign="top">State (if applicable):</td>
<td><input name="State" value="CA" size="40"></td></tr>
<tr><td align="right" valign="top">ZIP Code (if applicable):</td>
<td><input name="ZIP" value="" size="40"></td></tr>
<tr><td align="right" valign="top">Phone Number (if applicable):</td>
<td><input name="Phone Number" value="" size="40"></td></tr>
<tr><td align="right" valign="top">Category:</td>
<td><%Category%></td></tr>
<tr><td align="right" valign="top">Description:</td>
<td><textarea wrap="virtual" name="Description" value="" rows="3" cols="42"></textarea></td></tr>
<TD align="right" valign="top"><FONT face="Verdana, Helvetica, Arial" size="2" color="006699"><strong>Keywords: </STRONG><%Key%></TD>
<TD><INPUT TYPE="Key" NAME="Key" VALUE="NO"></TD></TR>
<tr><td align="right" valign="top">Contact Name:</td>
<td><input name="Contact Name" value="" size="40"></td></tr>
<tr><td align="right" valign="top">Contact Email:</td>
<td><input name="Contact Email" value="" size="40"></td></tr>
<tr><td align=right valign="top">Comments:</td>
<td><textarea wrap="virtual" name="Comments" value="$in{'Comments'}" rows="3" cols="42"></textarea></td></tr>
<tr><td align="right" valign="top">Username:</td>
<td><input name="Username" value="" size="20"></td></tr>
<tr><td align="right" valign="top">Password:</td>
<td><input name="Password" value="" size="20"></td></tr>
<tr><td align="right" valign="top">Image 1</td>
<td><input name="Image_1" type="file" value="" /><input type="hidden" name="Image_1_Transfer" value="<%Image_1%>" /></td></tr>

<tr><td align="right" valign="top">Caption 1:</td>
<td><textarea wrap="virtual" input name="Caption_1" value="" rows="3" cols="42"><%Caption_1%></textarea></td></tr>

<tr><td align="right" valign="top">Image 2</td>
<td><input name="Image_2" type="file" value="" /><input type="hidden" name="Image_2_Transfer" value="<%Image_2%>" /></td></tr>
<tr><td align="right" valign="top">Caption 2:</td>
<td><textarea wrap="virtual" input name="Caption_2" value="" rows="3" cols="42"><%Caption_2%></textarea></td></tr>


<tr><td></td><td><input type="SUBMIT" value="Add Resource"></td></tr>
</table>
</div>
</form>
and here is the add.cgi

sub main {
# --------------------------------------------------------
# change for upload mod >
# local (%in) = &parse_form;
local (%in) = &parse_me_and_upload;
# We are processing the form.
if (keys %in != 0) {
&process_form;
}
# Otherwise we are displaying the form (in site_html.pl).
else {
if ($db_single_category) {
my %is_valid = map { $_ => 1 } &category_list;
$ENV{'HTTP_REFERER'} =~ s,/[^/]+\.[^/]+$,,;
$ENV{'HTTP_REFERER'} =~ m,$build_root_url/(.+?)/?$,;
$is_valid{$1} ? &site_html_add_form ($1) : &site_html_add_form ();
}
else {
&site_html_add_form ();
}
}
}
sub process_form {
# --------------------------------------------------------
my ($key, $status, $line, $output);
# Check the referer.
if (@db_referers and $ENV{'HTTP_REFERER'}) {
$found = 0;
foreach (@db_referers) {
$ENV{'HTTP_REFERER'} =~ /$_/i and $found++ and last;
}
if (!$found) {
&site_html_add_failure ("Auto submission is not allowed in this directory. Please visit the site to add your entry.");
return;
}
}
# This will set system fields like Validated to their proper values.
foreach $key (keys %add_system_fields) {
$in{$key} = $add_system_fields{$key};
}

# Set modified and added date variables to today's date.
$in{$db_cols[$db_dateadded]} = $in{$db_cols[$db_modified]} = &get_date;

open (ID, "<$db_links_id_file_name") or &cgierr("error in process_form. unable to open id file: $db_links_id_file_name. Reason: $!");
$in{$db_key} = <ID> + 1; # Get next ID number
close ID;
# Validate the form input..
$status = &validate_record(%in);
if ($status eq "ok") {
open (DB, "<$db_file_name") or &cgierr("Error : unable to open db file: $db_file_name. Reason: $!");
$found = 0;
LINE: while (<DB>) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
chomp;
@data = &split_decode($_);
if ($data[$db_user] eq $in{'Username'}) {
$found = 1;
last LINE;
}
}
close DB;
$found and &site_html_add_failure("Username already exists.") and return;
# Update the counter.
open (ID, ">$db_links_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_links_id_file_name. Reason: $!");
flock(ID, 2) unless (!$db_use_flock);
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock
$pass = $in{'Password'};
$in{'Password'} = reverse $pass;
# Print out the validate input to a "validation database" where it is stored until
# the admin decides to add it into the real database.
open (VAL, ">>$db_valid_name") or &cgierr("error in add_record. unable to open validate file: $db_valid_name. Reason: $!");
flock(VAL, 2) unless (!$db_use_flock);
print VAL &join_encode(%in);
close VAL; # automatically removes file lock
# Send the admin an email message notifying of new addition.
&send_email;
# Send the visitor to the success page.
use CGI qw(:standard);
if ($in{'Contact Name'} =~ /\w+\s\w+/) {
split /\s/, $in{'Contact Name'};
$name = $_[0];
} else {
$name = $in{'Contact Name'};
}
$ck = cookie( -name => "WelcMSG",
-value => $name,
-expires => '+5yr',
-domain => ''
);
print header ( -cookie => $ck );
&site_html_add_success;
}
else {
&site_html_add_failure($status);
}
}
sub send_email {
# --------------------------------------------------------
# Sends an email to the admin, letting him know that there is
# a new link waiting to be validated. No error checking as we don't
# want users to see the informative &cgierr output.
# Check to make sure that there is an admin email address defined.
$db_admin_email or &cgierr("Admin Email Address Not Defined in config file!");
my $to = $db_admin_email;
my $from = $in{$db_cols[$db_contact_email]};
my $subject = "Addition to Database: $in{'Title'}\n";
my $msg = qq|
The following link is awaiting validation:
Title: $in{'Title'}
URL: $in{'URL'}
Category: $in{'Category'}
Description: $in{'Description'}
Contact Name: $in{'Contact Name'}
Contact Email: $in{'Contact Email'}
Comments: $in{'Comments'}
Address: $in{'Street Address url'}
City: $in{'City url'}
State: $in{'State'}
ZIP: $in{'ZIP'}
Phone Number: $in{'Phone Number'}
Image_1: $original{'Image_1'}
Caption_1: $original{'Caption_1'}
Image_2: $original{'Image_2'}
Caption_2: $original{'Caption_2'}

Remote Host: $ENV{'REMOTE_HOST'}
Referer: $ENV{'HTTP_REFERER'}
Remote Addr: $ENV{'REMOTE_ADDR'}

To validate, please go to:
$db_script_url

Sincerely,
Links Manager.
|;
# Then mail it away!
require "$db_lib_path/Mailer.pm";
my $mailer = new Mailer ( { smtp => $db_smtp_server,
sendmail => $db_mail_path,
from => $from,
subject => $subject,
to => $to,
msg => $msg,
log => $db_mailer_log
} ) or return;
$mailer->send or return;
}
# upload mod >
sub parse_me_and_upload {
#-------------------------------------
use CGI qw(:standard);
my (@pairs, %in);##
my ($buffer, $pair, $name, $value);##
my ($fnum,$file_field,$file_name,$ext,$bytes_count,$size,$buff);
my ($num)=1;
my ($IN) = new CGI;
# create a hash for all input - name => value
for ($IN->param) { $in{$_} = $IN->param($_) }
# loop through the input
for (keys %in) {
# if the field name matches File(DIGIT) this is a file upload field...
if (/^(Image_\d*)$/i) {
# establish and number associative files in array
# $in{$file_field} = name of file being uploaded from form.
# $1 = Form element name ie. File1
# Increment the form element counter
$fnum = $num++;
# do some stuff that I've yet to understand.
$file_field = $1;
$in{$file_field} =~ /([^\/\\]+)$/ and $file_name = $1;
# If the File(X) field is empty
if (length($in{$file_field}) < 1 ) {
# just to copy the value of File(X)Transfer
# into $in{$file_field}. This will either be an empty string or
# the previously uploaded filename.
$in{$file_field} = "$in{'Image_' . $fnum . '_Transfer'}";
# Other wise the File(x) form upload element has something in it.
}
else{
## LT added 'if delete_image' here...
if (($in{'image_' . $fnum . '_delete'} eq "yes") &&
(length($in{'Image_' . $fnum . '_Transfer'}) > 1)) {
# So first remove any previously uploaded files in that file(x) position
unlink "$upload_path/$in{'Image_' . $fnum . '_Transfer'}";
} # close LT 'if delete_image'
else { #LT
&site_html_add_failure("To replace an existing image you need to check the \'delete image\' box.") and return;
} # close LT 'else'
# Now let's go ahead and upload that new file
# changed to append Link ID instead of random number >
$file_name = $in{$db_key} . $file_name; # Prevent files being overwritten by appending Link ID
# < changed to append Link ID
open U, ">$upload_path/$file_name" or &cgierr("Can't open $upload_path/$file_name : $!");
binmode U; # needed for Windows servers
while ($bytes_count = read($in{$file_field},$buff,2096)) {
$size += $bytes_count;
print U $buff;
}
close U;
$in{$file_field} = $file_name;
# create an array of upload fields so we can unlink if there's a booboo
push @uploads, $in{$file_field}
}
}
}
# return the %in hash as normal
return %in;
}
# < upload mod
I haven't tried the modify part yet, Somebody please help.
Subject Author Views Date
Thread Here's a working image upload mod! PerlFlunkie 22256 Feb 17, 2005, 9:55 PM
Thread Re: [PerlFlunkie] Here's a working image upload mod!
PerlFlunkie 21896 Feb 17, 2005, 11:11 PM
Thread Re: [PerlFlunkie] Here's a working image upload mod!
jayman 21626 Nov 4, 2005, 5:57 AM
Thread Re: [jayman] Here's a working image upload mod!
PerlFlunkie 21636 Nov 4, 2005, 2:40 PM
Thread Re: Here's a working image upload mod!
LILHOMIE 20967 Nov 8, 2009, 4:50 PM
Post Re: [LILHOMIE] Here's a working image upload mod!
LILHOMIE 20961 Nov 8, 2009, 7:50 PM
Thread Re: [LILHOMIE] Here's a working image upload mod!
PerlFlunkie 20982 Nov 8, 2009, 8:01 PM
Post Re: [PerlFlunkie] Here's a working image upload mod!
LILHOMIE 20989 Nov 8, 2009, 9:13 PM
Thread Re: [PerlFlunkie] Here's a working image upload mod!
LILHOMIE 21017 Nov 8, 2009, 10:16 PM
Thread Re: [LILHOMIE] Here's a working image upload mod!
PerlFlunkie 20952 Nov 9, 2009, 9:50 AM
Thread Re: [PerlFlunkie] Here's a working image upload mod!
LILHOMIE 20898 Nov 11, 2009, 6:53 AM
Thread Re: [LILHOMIE] Here's a working image upload mod!
PerlFlunkie 20887 Nov 11, 2009, 8:58 AM
Thread Re: [PerlFlunkie] Here's a working image upload mod!
LILHOMIE 20919 Nov 11, 2009, 3:40 PM
Thread Re: [LILHOMIE] Here's a working image upload mod!
LILHOMIE 20875 Nov 11, 2009, 3:53 PM
Thread Re: [LILHOMIE] Here's a working image upload mod!
PerlFlunkie 20859 Nov 11, 2009, 5:15 PM
Thread Re: [PerlFlunkie] Here's a working image upload mod!
LILHOMIE 20824 Nov 12, 2009, 9:13 PM
Thread Re: [LILHOMIE] Here's a working image upload mod!
PerlFlunkie 20755 Nov 14, 2009, 3:06 PM
Post Re: [PerlFlunkie] Here's a working image upload mod!
PerlFlunkie 20460 Nov 24, 2009, 5:38 PM
Post Re: [LILHOMIE] Here's a working image upload mod!
LILHOMIE 20912 Nov 11, 2009, 5:12 PM