Gossamer Forum
Home : General : Perl Programming :

trying to upload a file

Quote Reply
trying to upload a file
want to write a file for email attachment
Code:
sub get_email_attachment {
#-----------------------------------------------
# under construction 10/30/2017 ???????????????????????????????

$| = 1;

my ($csvfileloc, $csvfilename, $filekey);

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

if ($filekey =~ /([^\/\\]+)$/) {
$csvfilename = $in{'Filename'};
$csvfileloc = $db_csv_path . '/' . $in{'Filename'};
}
else {
return "You attempted to upload <strong>$filekey</strong> 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.";
}

open(OUTFILE, ">$csvfileloc") or die "Unable to open attachment location ($csvfileloc).";

if (!open(OUTFILE, ">$db_csv_path\/$csvfilename")) {
return "There was an error opening '$db_csv_path\/$csvfilename' 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;
}

close($filekey);
close(OUTFILE);

chmod (0666, "$db_csv_path\/$csvfilename");

return("ok");

}
it is creating the filename in the correct folder but it is 0 bytes. i copied the code from a routine that works in another part of the script. this is entire sub i copied from
Code:
sub validate_upload_single {
# --------------------------------------------------------
my ($filekey,$filename,$newfilename,$extlength,$filehandle,$totalbytes,$buffer,$bytes,@extensions,@ext);

$| = 1;

$filekey = $query->param("Filename");
$newfilename = $in{$db_key};
#### deleted following because n/a
if (!$ALLOWED_EXT) { # 01/21/2017
return "Allowed file types have not been defined in configuration.";
}

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>
/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.<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.";
}
#### end deleted section
if ($filekey =~ /([^\/\\]+)$/) {
$filename = $1;
### not testing extensions so deleted following
$extlength = length($filename) - rindex($filename,".");
$filename = $newfilename . lc(substr($filename,-$extlength,$extlength));
unless (lc($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";
}
### end delete
}
else {
return "You attempted to upload <strong>$filekey</strong> 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.";
}
### nothing to delete so deleting this section
opendir (GRAPHIC, "$SAVE_DIRECTORY") or &cgierr("unable to open directory in delete records: $SAVE_DIRECTORY. Reason: $!");
@files = readdir(GRAPHIC);
closedir (GRAPHIC);
$file_test = $rec{$db_key};
foreach $file (@files) {
if ($file =~ /^$file_test\./) {
unlink ("$SAVE_DIRECTORY/$file");
}
}
#### end deleted section

### added a line here to open

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;
}

close($filekey);
close(OUTFILE);

chmod (0666, "$SAVE_DIRECTORY\/$filename");
### not testing filesize so deleted following
if ($totalbytes > $MAXIMUM_UPLOAD && $MAXIMUM_UPLOAD > 0) {
unlink "$SAVE_DIRECTORY\/$filename";
return "Filename<BR>
You have reached your upload limit.<BR>
Your file contains <strong>$BytesRead $totalbytes</strong> bytes.<BR>
This exceeds the maximum limit of <strong>$MAXIMUM_UPLOAD</strong> bytes.<BR>
Your file was not saved.<BR>
Please try again.";
}

return "ok";
}

what's wrong??? thanks!
Quote Reply
Re: [delicia] trying to upload a file In reply to
Are you using a multipart form?

https://www.w3schools.com/...att_form_enctype.asp

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] trying to upload a file In reply to
i was hoping it was something that easy. thanks!!!
Quote Reply
Re: [delicia] trying to upload a file In reply to
hehe you wouldn't believe a number of times I've gone looking really deep for an issue, to find out its something as silly/simple as that :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!