Gossamer Forum
Home : Products : DBMan : Customization :

Upload image/picture to you database in db.cgi

Quote Reply
Upload image/picture to you database in db.cgi
* In (default.cfg) Add:
Filename => [10,'alpha',0,255,1,'','']
* In html_record_form (html.pl) Add:
<TR><TD>Browse Picture:</TD>
<TD><INPUT TYPE="FILE" NAME="Filename" SIZE="50"></TD></TR>
* First thing in (db.cgi) Add:
use CGI;
$query = new CGI;
* Replace Parse_Form (db.cgi) With:
sub parse_form {
my (%in);
my ($buffer, $pair, $name, $value);

PAIR: foreach $name ($query->param()) {
$value = $query->param("$name");
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
if ($value eq "---") { next PAIR; }
(exists $in{$name}) ?
($in{$name} .= "~~$value") :
($in{$name} = $value);
return %in;
* New sub validate_upload (db.cgi):
sub validate_upload {
my ($filekey,$filename,$newfilename,$extlength,$filehandle,$totalbytes,$buffer,$bytes,@extensions,@ext);

$| = 1;

$filekey = $query->param("Filename");
$newfilename = $db_userid;

if (!(-e $SAVE_DIRECTORY)) {
return "The directory doesn't exist. Make sure that this directory is a complete path name,\nnot a URL or something similar. It should look similar to\n/home/username/public_html/uploads";
if (!(-W $SAVE_DIRECTORY)) {
return "The directory isn't writable. Make sure that this directory is writable by all users.\nAt your UNIX command prompt, type chmod 777 $SAVE_DIRECTORY";
if (!(-d $SAVE_DIRECTORY)) {
return "The directory you specified isn't really a directory.\nMake sure that this is indeed a directory and not a file.";

if ($filekey =~ /([^\/\\]+)$/) {
$filename = $1;
$extlength = length($filename) - index($filename,".");
$filename = $newfilename . lc(substr($filename,-$extlength,$extlength));
unless ($filename =~ /$ALLOWED_EXT/) {
$ALLOWED_EXT =~ s/\\//g;
$ALLOWED_EXT =~ s/\$//g;
@ext = split (/\Q|\E/o,$ALLOWED_EXT);
$ALLOWED_EXT = join(" or ",@ext);
return "Only files with the following extension(s) are allowed: $ALLOWED_EXT";
} else {
return "You attempted to upload <B>$filekey</B> that isn't properly formatted. Please rename the file on your computer, and attempt to upload it again. Files may not have forward or backward slashes in their names. Also, they may not be prefixed with one (or more) periods.";

$ALLOWED_EXT =~ s/\\.//g;
$ALLOWED_EXT =~ s/\$//g;

@extensions = split (/\Q|\E/o,$ALLOWED_EXT);
foreach $extension (@extensions) {
(-e "$SAVE_DIRECTORY/$newfilename.$extension") && (unlink "$SAVE_DIRECTORY/$newfilename.$extension")
if (!open(OUTFILE, ">$SAVE_DIRECTORY\/$filename")) {
return "There was an error opening '$SAVE_DIRECTORY\/$filename' for Writing.\n";

binmode(OUTFILE); # This is needed to work on Windows/NT platforms.

while ($bytes = read($filekey,$buffer,1024)) {
$totalbytes += $bytes;
print OUTFILE $buffer;


chmod (0666, "$SAVE_DIRECTORY\/$filename");

if ($totalbytes > $MAXIMUM_UPLOAD && $MAXIMUM_UPLOAD > 0) {
unlink "$SAVE_DIRECTORY\/$filename";
return "Filename<BR>
You have reached your upload limit.<BR>
Your file contains <B>$BytesRead $totalbytes</B> bytes.<BR>
This exceeds the maximum limit of <B>$MAXIMUM_UPLOAD</B> bytes.<BR>
Your file was not saved.<BR>
Please try again.";

return "ok";
* Change sub add_record (db.cgi):
$status = &validate_record;
if ($status eq "ok") { $status = &validate_upload; } #Validate Picture
* Change html_add_XXX & html_modify_XXX(db.cgi):
<FORM action="$db_script_url" method="POST">
<FORM ENCTYPE="multipart/form-data" action="$db_script_url" method="POST">
**** You can change all <Form Action to <Form ENCTYPE="multipart/form-data" action
Now the user have to upload a picture to his/her record.

Regards, Jim
Mail me if you need help with the code above.


//Jim Kangosjärvi
System Designer and VisualBasic Developer

[This message has been edited by JFK II (edited April 28, 2000).]
Quote Reply
Re: Upload image/picture to you database in db.cgi In reply to
Thank you. I'll try it out and, if you don't mind, create a text file for the mod.