Gossamer Forum
Home : Products : DBMan : Customization :

File Upload Mod Divide By Zero

Quote Reply
File Upload Mod Divide By Zero
I modified the files per the file upload mod. When I try and add an entry with a pic attached I get a Divide By Zero error. If I submit the new record without a image file attached it works fine. Any Ideas??

fatal error: Illegal division by zero at C:\InetPub\wwwroot\indyclubs\cgi-bin\db.cgi line 205.

Line 205 is part of the cub validate_upload section.

sub validate_upload {
# --------------------------------------------------------
my ($filekey,$filename,$extlength,$filehandle,$totalbytes,$buffer,$bytes,@extensions,@ext,
$newdirname,$dirsuccess,$num_files,$prev_files,$prev_bytes);

$| = 1;

if (!(-e $SAVE_DIRECTORY)) {
return "The directory doesn't exist. Make sure that this directory is a complete path name,<BR>
not a URL or something similar. It should look similar to<BR>";


} ******* This is line 205 *******


if (!(-W $SAVE_DIRECTORY)) {
return "The directory isn't writable. Make sure that this directory is writable by all users.<BR>
At your UNIX command prompt, type chmod 777 $SAVE_DIRECTORY";
}
if (!(-d $SAVE_DIRECTORY)) {
return "The directory you specified isn't really a directory.<BR>
Make sure that this is indeed a directory and not a file.";
}
$newdirname = $in{$db_key};
if (!(-e $SAVE_DIRECTORY/$newdirname)) {
$dirsuccess = mkdir "$SAVE_DIRECTORY/$newdirname", 0777;
}
else {
opendir (GRAPHIC, $SAVE_DIRECTORY/$newdirname) or &cgierr("unable to open directory. Reason: $!");
@files = readdir(GRAPHIC);
closedir (GRAPHIC);
foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
++$prev_files;
@stats = stat "$SAVE_DIRECTORY/$newdirname/$file";
$prev_bytes +=$stats[7];
}
}

foreach $key (sort {$a <=> $b} $query->param()) {
next if ($key =~ /^\s*$/);
next if ($query->param($key) =~ /^\s*$/);
next if ($key !~ /^file-to-upload-(\d+)$/);
$Number = $1;
++$num_files;
if ($query->param($key) =~ /([^\/\\]+)$/) {
$filename = $1;
$File_Handle = $query->param($key);
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.";
}

if (!open(OUTFILE, ">$SAVE_DIRECTORY\/$newdirname\/$filename")) {
return "There was an error opening '$SAVE_DIRECTORY\/$newdirname\/$filename' for Writing.\n";
}

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

undef $BytesRead;
undef $Buffer;

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

push(@Files_Written, "$SAVE_DIRECTORY\/$newdirname\/$filename");

close($File_Handle);
close(OUTFILE);

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

}

if (($totalbytes + $prev_bytes) > $MAXIMUM_UPLOAD && $MAXIMUM_UPLOAD > 0) {
foreach $written (@Files_Written) {
unlink "$written";
}
return "You have exceeded your upload limit for this record.<BR>
Your files contain <B>$totalbytes</B> bytes.<BR>
Combined with previous uploads totaling <B>$prev_bytes</B>, this
exceeds the maximum limit of <B>$MAXIMUM_UPLOAD</B> bytes per record.<BR>
Your files were not saved.<BR>
Please try again.";
}
if (($num_files + $prev_files) > $MAXIMUM_FILES) {
foreach $written (@Files_Written) {
unlink "$written";
}
return "You have exceeded your upload limit for this record.<BR>
You uploaded <B>$num_files</B> files.<BR>
Combined with previous <B>$prev_files</B> uploads, this
exceeds the maximum limit of <B>$MAXIMUM_FILES</B> files per record.<BR>
Your files were not saved.<BR>
Please try again.";
}
return "ok";
}
Quote Reply
Re: [lab99] File Upload Mod Divide By Zero In reply to
You get that error when dividing something by 0....I can't see anything in that code but if you can find it then just wrap it with an eval block...eg instead of:

$something / 0;

You'd do:

eval { $something / 0 };
Quote Reply
Re: [Paul] File Upload Mod Divide By Zero In reply to
I found what I think is the problem but have no idea how to fix it. in the *.cfg file the save directory variable is set to $SAVE_DIRECTORY = "c:/inetpub/wwwroot/indyclubs/pics"; The problem is the / are being read as divide signs not directory seperators. I tried it without the C: just /xxx/xxx/xxx with the same divide by zero error. I changed the / to a \ and got an error stating the directory wasn't writable. It ignored the \ and showed the directory as inetpubwwwrootindyclubspics. The script is running on under NT 4. All the other variables are defined with the c:/xxx/xxx without any problems. Any ideas?
Quote Reply
Re: [lab99] File Upload Mod Divide By Zero In reply to
Just a Scientific Wild A** Guess, but could you escape the the slashes?

c:\/inetpub\/wwwroot\/indyclubs\/pics

Didn't read all of your post so feel free to ignore this if not even close to being on target.

Gotta go.
Quote Reply
Re: [lab99] File Upload Mod Divide By Zero In reply to
Try changing / to \\
Quote Reply
Re: [lab99] File Upload Mod Divide By Zero In reply to
I had the same problem but solved it by putting the " " in the following lines found in the sub validate_upload routine:

old line:

else {
opendir (GRAPHIC, $SAVE_DIRECTORY/$newdirname) or &cgierr("unable to open directory. Reason: $!");
@files = readdir(GRAPHIC);

new line:

else {
opendir (GRAPHIC, "$SAVE_DIRECTORY/$newdirname") or &cgierr("unable to open directory. Reason: $!");
@files = readdir(GRAPHIC);

Jim
Jim McFall
Programmer
Web Guy Productions
Quote Reply
Re: [jmcfall] File Upload Mod Divide By Zero In reply to
I'm not sure that would fix it :) ....the error is to do with dividing something by zero. The code in the original post doesn't seem to show any divisions so that makes me think it is the wrong chunk of code.

Last edited by:

Paul: Aug 5, 2002, 3:03 AM
Quote Reply
Re: [Paul] File Upload Mod Divide By Zero In reply to
It solved my having the same problem.

I might be wrong (I've only been learning Perl for less than two weeks) but this is what I see the problem to be. Correct me please if have misinterpreted this.

Without the Double Quoted Strings (" "), the statement is $scalar1 / $scalar2 (a mathematical statement). If $scalar2 is empty or undefined, this could get a division by zero error.

With the Double Quoted Strings (" "), I believe the program actually sees the / as a text character rather than the division operator.

Again, if I'm way out in left field, please let me know.Smile
Jim McFall
Programmer
Web Guy Productions
Quote Reply
Re: [jmcfall] File Upload Mod Divide By Zero In reply to
Oh no, sorry you are totally right, I completely misunderstood your post above...I thought you were saying that the blank line in the second example fixed the problem Blush

Last edited by:

Paul: Aug 5, 2002, 8:41 AM
Quote Reply
Re: [Paul] File Upload Mod Divide By Zero In reply to
Thanks for this fix. I just fixed mine with this issue. To add to the notes. There was a post in 2000 that mentioned that the db_key might be an alpha rather than a numer. Mine is an alpha. The quotes added made the difference.

What a wonderful invention, the support forums!

Chris