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!
Subject Author Views Date
Thread trying to upload a file delicia 5111 Oct 30, 2017, 1:15 PM
Thread Re: [delicia] trying to upload a file
Andy 5016 Oct 30, 2017, 11:00 PM
Thread Re: [Andy] trying to upload a file
delicia 5004 Oct 31, 2017, 7:09 AM
Post Re: [delicia] trying to upload a file
Andy 4999 Oct 31, 2017, 7:11 AM