The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Perl : Filehandles in 2D Array
Discuss Perl : Filehandles in 2D Array in the Perl Programming forum on Dev Shed. Perl : Filehandles in 2D Array Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 12th, 2013, 11:08 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 25 sec
Reputation Power: 0
|
|
Perl : Filehandles in 2D Array
Oh man, I'm hoping someone out there might be able to help me out with this one.
I've been sorta 'Frankensteining' some code together from an old script and some examples on the web in an attempt to get file attachments to work with a webform. The idea is that the site visitor is able to select multiple files from their computer, have them upload to a server and then have the script attach them to an email and send it off.
The script worked great when there was just one file selected but I now need to have the 'multiple' option enabled in order for users to be able to (obviously) select multiple files for upload. When it was just one file, I ran a check to make sure the filename was valid and if it wasn't, it would rename it with only acceptable characters. This still appears to be working just fine with the FOR loop in here.
Where I seem to be having the most problems is at the OPEN (UPLOAD FILE...) - for some reason it isn't reading the array values as proper filehandles but is instead simply injecting the filename as a string into the files. If I upload a Doc file, all I see is the name of the document inside the file.
I would be forever grateful if someone were able to point me in the right direction on this. Thanks!
This is the section of code that is giving me grief:
Code:
# ------ These are the modules used at the top of my script -----------
use strict;
use CGI;
use CGI::Carp;
use File::Basename;
use MIME::Base64;
use MIME::Lite;
use MIME::Types qw(by_suffix by_mediatype import_mime_types);
use Encode::Byte;
use Captcha::reCAPTCHA;
#---------------------------------------------------------------------------
my $upload_dir = '\\MyDirectory\SubDirectory';
my $filename_characters = 'a-zA-Z0-9_.-';
my @upload;
@upload[0] = [$q->param("Uploads")]; # set first row of 2D array to the webform filenames selected.
@upload[1] = [$q->upload("Uploads")]; # set the second row of the 2D array to the webform filehandles
for (my $i=0; $i<=$#{$upload[0]}; ++$i)
{
# Validate file name and modify if needed
my ($filename,undef,$ext) = fileparse($upload[0][$i],qr{\..*});
# append extension to filename
$filename .= $ext;
# convert spaces to underscores "_"
$filename =~ tr/ /_/;
# remove illegal characters
$filename =~ s/[^$filename_characters]//g;
# satisfy taint checking
if ($filename =~ /^([$filename_characters]+)$/)
{
$filename = $1;
}
else
{
error("The filename is not valid. Filenames can only contain these characters: $filename_characters")
}
my ($mime_type, $encoding) = by_suffix($filename); # Get file Mime Type and Encoding for the email attachement
# Open file, write data to file, close file.
open (UPLOADFILE, ">$upload_dir/$filename") or error("Can't open/create \"$upload_dir/$filename\": $!");
#THIS IS WHERE I'M HAVING PROBLEMS
binmode UPLOADFILE;
while ( <$upload[1][$i]> ) {
print UPLOADFILE;
}
close UPLOADFILE;
# Attach file to email
$msg->attach( Type =>$mime_type,
Path =>"$upload_dir\\$filename",
Filename =>"$filename",
Encoding =>$encoding);
} # Loop back for next file.
$msg->send('smtp', 'smtp.server.com', Timeout=>30);
|

February 12th, 2013, 01:16 PM
|
|
|
Have you read this portion of the CGI documentation?
|

February 12th, 2013, 01:21 PM
|
|
|
|
Do you understand the difference between these and when to use one or the other?
@upload[0]
$upload[0]
|

February 12th, 2013, 02:15 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 25 sec
Reputation Power: 0
|
|
Hmmm... apparently not.
Thanks for the link. I think I found a solution though after all that frustration. I just tested assigning the array value to another variable before running the write.
Something like this:
Code:
my $fh = $upload[1][$i]; # Set file handle to variable
binmode UPLOADFILE; # Write file to server
while ( <$fh> ) {
print UPLOADFILE;
}
close UPLOADFILE;
I think I had tried @upload[1][$i] in previous attemps though my syntax may have been wrong at that time.
Regardless - the struggle paid off.
Thanks for the direction though!
Quote: | Originally Posted by FishMonger Do you understand the difference between these and when to use one or the other?
@upload[0]
$upload[0] |
|

February 12th, 2013, 02:16 PM
|
|
|
Quote: | but I now need to have the 'multiple' option enabled in order for users to be able to (obviously) select multiple files for upload |
You need to use separate input fields of type 'file', not a single dropdown where 'multiple' selection is enabled.
|

February 12th, 2013, 02:19 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 25 sec
Reputation Power: 0
|
|
Actually, I've been able to make this method working using
Code:
<input type="file" name="Uploads" id="uploadFile" multiple>
as my webform input tag. It's not a drop down, but it allows the user to select as many files from a folder as they like.
Quote: | Originally Posted by FishMonger You need to use separate input fields of type 'file', not a single dropdown where 'multiple' selection is enabled. |
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|