Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid

hidden variables with encType="multipart/form-data"

by virtualweb (Sexton)
on Jul 26, 2009 at 05:10 UTC ( #783268=perlquestion: print w/ replies, xml ) Need Help??
virtualweb has asked for the wisdom of the Perl Monks concerning the following question:

Not able to pass variables data:

Hello .. Im building a form that uploads files with encType="multipart/form-data but on the same form I need to pass other variables with hidden fields.

The files are uploading fine but Im not getting the values of the hidden fields.

How can I get hidden field values along with uploaded files in the same form..??

This is my form code:

<form action="" encType="multipart/form-data" method="Po +st"> <input type=file name="FILE"> <input type="hidden" name="URL" value="$URL"> <input type="hidden" name="Alias" value="$Alias"> <INPUT type=submit value="Upload File"> </form>

Im including the free perl script i downloaded from the internet that handles the upload below

Thanx beforehand
Virtual Web

#!/usr/bin/perl use GD; my $req = new CGI; # CONFIGURE VARIABLES $jpeg_quality = 75; # # $Data = "pictures"; # On your server, create a directory where this program will wr +ite the files # to. Make sure you CHMOD this directory to 777. If you do NOT +specify a $Data # directory, the program will attempt to write to the web root +directory. # NOTE: YOU SHOULD ALWAYS SPECIFY A DIRECTORY TO STORE THE UPLO +AD @good_extensions = ('gif', 'jpg', 'jpeg','png', 'PNG','GIF', 'JPG', 'J +PEG'); # If you want to limit the types of extension that can be uploa +ded, specify them # here by adding them to the array. For example, if you wanted +to permit only # the upload of gif's, jpg's and png's, then you would set the +above array to # look like this: # @good_extensions = ('gif', 'jpg', 'jpeg', 'png'); # @bad_extensions = (); # If you want to permit the upload of all file types with only +certain exceptions, # then specify those extensins in the bad_extensions array. Th +is means that if set # this array to contain .exe, .pl, .cgi files, then the program +will only store a # file if the extension of that file is NOT found in this array +. # To set the array to exclude these sample extensions, you woul +d set it like this: # @bad_extensions = ('exe', 'cgi', 'pl'); # # NOTE: If you specify both @good_extensions and @bad_extension +s, then # the settings in @bad_extensions will be ignored and the progr +am will # use @good_extensions as it's refrence. #$redirect = ""; # When the upload of files is complete, the program must print +someting out on the # browser screen. Set the $redirect variable to the full URL (d +on't forget the http://) # that you want the person taken to once the program is finishe +d. If you don't specify # a URL here, the program will print out a simple upload summar +y page. $max_size = "2000"; # Set the maximum size of each file that is permitted. For exam +ple, if you only want # files to be uploaded that are under 50Kb in size, set the val +ue to: # $max_size = 50; # If you set the value to zero, remove it or comment it out, th +en the size of the # uploaded file will NOT be checked. $max_num_files = 5; # You must specify the maximum number of files that can be uplo +aded at one time. You # can set this to any number you want but be realistic. The lim +it before the server # times out will depend on the maximum size of the upload. I ha +ve tested this program # with ASCII files up to 8MB in size successfully but that was +on a particularly # robust server. I recommend that you set this no higher than 5 + if you are going to # be using this for larger binary files such as images or execu +tables or word docs, etc. # If you remove, comment out or set this value to zero, the pro +gram will default the # value to 1 file. # ###################################################################### +################# # # DO NOT EDIT ANYTHING BELOW THIS LINE # UNLESS YOU KNOW WHAT YOU ARE DOING # if(($ENV{'QUERY_STRING'} =~ /^debug/) && !$no_debug) { print "Pragma: no-cache\nContent-type: text/html\n\n"; print "<TITLE>PSUpload Demonstration Upload Program - Debug Mode</ +TITLE></HEAD><BODY bgcolor =#707190 TEXT=\"#330066\" LINK=\"#336666\" ALINK=\"#336666\" VLINK=\"# +336666\" BGCOLOR=\"#FFFFFF\"><FONT SIZE=\"2\" FACE=\"verdana, arial, helvetica\ +">\n"; print"<hr>$URL<br>$Half_Credit_Limit<br>$req->param('URL')<hr>"; print "<CENTER><B><H2>Charity Ware's PSUpload Program</H2></B><BR> +<BR><TABLE BORDER=0><TR><TD COLSPAN=2><FONT SIZE=\"2\" FACE=\"verdana, arial, helvetica\">\n"; print "<DL><DT><B>Your web root directory appears to be located at:</B><DD>$ENV{'DOCUMENT_ROOT'}<BR><BR><DT><B>You specified directory + for storing the uploads is:</B><DD>$Data<BR><BR><DT><B>Your specified directory...</B><DD>\n"; if(-d $Data) { print "...appears to be a valid directory.<BR><BR>Make sure this \ +$Data directory is CHMOD 777.\n"; } else { print "...does not appear to be a valid directory.<BR><BR>\n"; unless($Data =~ /^$ENV{'DOCUMENT_ROOT'}/) { print "The value you specified in the \$Data variable is incor +rect. Please<BR>correct your \$Data variable and run debug again.<BR><BR>\n"; } } if($Data =~ /\/$/) { print "<FONT COLOR=\"#FF0000\">NOTE: Your variable \$Data ends wit +h a trailing slash. Please<BR>remove this trailing slash, upload the program again<BR>and +run debug once more to see if you have a valid directory.</FONT><BR><BR>\n"; } print "</DL><BR><BR></FONT></TD></TR><TR><TD WIDTH=\"50%\" VALIGN= +\"TOP\"><FONT SIZE=\"2\" FACE=\"verdana, arial, helvetica\"><B>OS:</B><BR>$^O<BR><BR><B>Perl:</ +B><BR>$]</FONT></TD><TD VALIGN=\"TOP\"><FONT SIZE=\"2\" FACE=\"verdana, arial, helvetica\"><B> +Installed:</B><BR>"; my @inst = split(/\//, $ENV{'SERVER_SOFTWARE'}); print join("<BR>", @inst +); print"</FONT></TD></TR></TABLE><BR><BR><BR><BR></CENTER><BR><BR></FONT +></BODY></HTML>\n"; } else { use CGI; $max_num_files ||= 1; $Data ||= $ENV{'DOCUMENT_ROOT'}; undef @bad_extensions if @good_extensions; for(my $a = 1; $a <= $max_num_files; $a++) { # my $req = new CGI; if($req->param("FILE$a")) { my $file = $req->param("FILE$a"); my $filename = $file; $filename =~ s/^.*(\\|\/)//; $filename =~ s/ +/\_/g; my $proceed_type = 0; if(@good_extensions) { foreach(@good_extensions) { my $ext = $_; $ext =~ s/\.//g; if($filename =~ /\.$ext$/) { $proceed_type = 1; last; } } unless($proceed_type) { push(@was_not_good_type, $filename); } } elsif(@bad_extensions) { $proceed_type = 1; foreach(@bad_extensions) { my $ext = $_; $ext =~ s/\.//g; if($filename =~ /\.$ext$/) { $proceed_type = 0; last; } } unless($proceed_type) { push(@was_a_bad_type, $filename); } } else { $proceed_type = 1; } if($proceed_type) { if(open(OUTFILE, ">$Data/$filename")) { while (my $bytesread = read($file, my $buffer, 2000)) { print OUTFILE $buffer; } close (OUTFILE); push(@file_did_save, $filename); } else { push(@did_not_save, $filename); } } if($max_size) { if((-s "$Data/$filename") > ($max_size * 2000)) { push(@was_too_big, $filename); unlink("$Data/$filename"); } } ### Image resize begin $im = GD::Image->new("$Data/$filename"); if(!$im){push(@gif_not, $filename);} else{ ($x,$y) = $im->getBounds(); $new_ord = 400; if ($x > $y) { $new_width = $new_ord; $new_height = $y * ($new_width * 100 / $x) / 100; } if ($x < $y) { $new_height = $new_ord; $new_width = $x * ($new_height * 100 / $y) / 100; }; if ($x == $y) { $new_height = $new_ord; $new_width = $new_ord; } if(($x > $new_ord) || ($y > $new_ord)){ $myImg = new GD::Image($new_width,$new_height,1); $myImg->copyResized($im,0,0,0,0,$new_width,$new_height,$x,$y +); open(F,">$Data/$filename"); print F $myImg->jpeg($jpeg_quality); close(F); } } ### Image resize end } } print "Pragma: no-cache\n"; if($redirect && ($redirect =~ /^http\:\/\//)) { print "Location: $redirect\n\n"; } else { print "Content-type: text/html\n\n"; print "<HEAD><TITLE>PSUpload Results</TITLE></HEAD><BODY bgcolor = +#707190><FONT FACE=\"verdana,helvetica,arial\" SIZE=2><BR><BR><CENTER><B><H2>Upload +Results</H2></B><HR WIDTH=\"65%\"><BR><BR>\n"; if(@file_did_save) {print "<B>The following file(s) were saved:<BR +><BR>\n"; print join("<BR>", @file_did_save); print "<BR><BR>\n"} if(@was_not_good_type) {print "<B>The following file(s) were not s +tored as their file extension<BR>did not match any of the valid extensions specified in th +e program:<BR><BR>\n"; print join("<BR>", @was_not_good_type); print "<BR><BR>\n"} if(@was_a_bad_type) {print "<B>The following files were not stored + as their file extension<BR>are on the list of extensions not permitted for upload:<B +R><BR>\n"; print join("<BR>", @was_a_bad_type); print "<BR><BR>\n"} if(@was_too_big) {print "<B>The following files were not stored as + their file size<BR>exceeded the maximum file size of $max_size Kb.:<BR><BR>\n"; p +rint join("<BR>", @was_too_big); print "<BR><BR>\n"} if(@gif_not) {print "<B>GIF resize not supported by GD<BR><BR>\n"; + print join("<BR>", @gif_not); print "<BR><BR>\n"} if(@did_not_save) {print "<B>The following files were not stored b +ecause the<BR>program could not open their destination file:<BR><BR>\n"; print join("<BR>", +@did_not_save);print "<BR><BR>\n"; if(!@file_did_save) {print "<FONT COLOR=\"RED\"><B>NOTE: C +heck to ensure that the \$Data variable reflects the correct<BR>absolute path to the directory + these files should be store in.</B></FONT><BR><BR>"} } print "<BR><BR><HR WIDTH=\"65%\"><BR><BR></BODY></HTML>\n"; } }

Comment on hidden variables with encType="multipart/form-data"
Select or Download Code
Re: hidden variables with encType="multipart/form-data"
by Anonymous Monk on Jul 26, 2009 at 07:19 UTC
    1st CGI object you create gets all submitted data, the 2nd object you create gets none, so only use the 1st object.

      Sorry... you lost me, I have three variables in my form: (FILE, URL, Alias). Is each variable an objetc..??

      Is there a better way of uploading a file and including hidden fields in the form not using encType="multipart/form-data"..??

      Thanx again

        Really? Looks like you've updated line 127 to #    my $req = new CGI; :)

        won't call method param, string interpolation doesn't work like that. Try
        print "<hr>", $req->Dump, "<hr>";
Re: hidden variables with encType="multipart/form-data"
by ikegami (Pope) on Jul 26, 2009 at 19:17 UTC

    The files are uploading fine but I'm not getting the values of the hidden fields.

    It makes absolutely no difference if you're uploading a file at the same time or not.

    #!/usr/bin/perl use strict; use warnings; use CGI; my $cgi = CGI->new(); print $cgi->header('text/plain'); print('URL=', $cgi->param('URL'), "\n"); print('Alias=', $cgi->param('Alias'), "\n"); my $fh = $cgi->param('FILE'); print("--\n"); print while <$fh>;

    One thing should verify is that you actually gave a value to the hidden fields.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://783268]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-04-17 06:55 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (440 votes), past polls