Mailing List Archive

File Upload
I am trying to upload a file. I am following the example presented in the
apache.org/embperl site.

As in the example, if the file handle isn't defined, I accept the file name
from the user.

print "<FORM METHOD=POST ENCTYPE='multipart/form-data'>";
print "<INPUT TYPE=FILE NAME='UserFile'>";

In the second pass, I display the data back to the user

while (@buffer = <$UserFile>) {print "<P> $buffer";}

and put up a button which will submit a hidden field to indicate the user
has accepted the data.

print "<FORM METHOD=POST ACTION='myprogram.mpl'>";
print "<INPUT TYPE=HIDDEN NAME='UserFile' VALUE='$UserFile'>";
print "<INPUT TYPE=HIDDEN NAME='userApproval' VALUE='Y'>";
print "<INPUT TYPE=SUBMIT VALUE='Post this data?'>";
print "</FORM>";

On my third pass, I have lost control of the file. The scalar $UserFile
has the path and file name, but I cannot make use of it. Although I never
opened the file to read it before,

@buffer = <UserFile>;
print "<P> \$buffer0 $buffer[0]";

now gives me nothing, and

print "<P> Error openning $UserFile \$UserFile" if (!open(FILE, '$UserFile'));

always fails.

Any suggestions?
Why didn't I have to open the file to read the data?

Thanks,
Jim




Jim Hamer

Downtown Data Processing Inc.
(416) 489-3298 home and office
jhamer@canada.com
Toronto, Ontario, Canada
http://webhome.idirect.com/~jhamer/
Re: File Upload [ In reply to ]
>
> In the second pass, I display the data back to the user
>
> while (@buffer = <$UserFile>) {print "<P> $buffer";}
>
> and put up a button which will submit a hidden field to indicate the user
> has accepted the data.
>
> print "<FORM METHOD=POST ACTION='myprogram.mpl'>";
> print "<INPUT TYPE=HIDDEN NAME='UserFile' VALUE='$UserFile'>";

You cannot pass a file within a hidden field, actualy here you pass only the
filename not the content of the file. You have to safe the file to a unique
name to the server and pass this name to the next request, but keep in mind
that anybody can play around with this name and sends back a different one.
You should make sure that, you don't create a security whole in this way. On
the third request you can use this filename and open the file localy.

> Why didn't I have to open the file to read the data?
>

Embperl (actualy CGI.pm in this case) has already opend the file for you

Gerald


-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925131
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------
Re: File Upload [ In reply to ]
Hi Jim,

At Wed, 7 Mar 2001 20:11:15 +0100, Gerald Richter <richter@ecos.de> wrote:
>>
>> In the second pass, I display the data back to the user
>>
>> while (@buffer = <$UserFile>) {print "<P> $buffer";}
>>
>> and put up a button which will submit a hidden field to indicate the user
>> has accepted the data.
>>
>> print "<FORM METHOD=POST ACTION='myprogram.mpl'>";
>> print "<INPUT TYPE=HIDDEN NAME='UserFile' VALUE='$UserFile'>";
>
>You cannot pass a file within a hidden field, actualy here you pass only the
>filename not the content of the file. You have to safe the file to a unique
>name to the server and pass this name to the next request, but keep in mind
>that anybody can play around with this name and sends back a different one.
>You should make sure that, you don't create a security whole in this way. On
>the third request you can use this filename and open the file localy.

Alternatively, you could read in the whole file and store it in the hidden field. The only disadvantages is with large files and/or binary data...

You could also look at using sessions, which are really easy to use with embperl...

Regards,

Wim Kerkhoff
Re: file upload [ In reply to ]
frag1.epl runs in a different namespace, where %fdat does not exist.
If you set EMBPERL_PACKAGE, it supposedly runs everything in that
namespace, but I've not yet gotten flawless results with this (I'm doing
a bunch of non-standard stuff; this could be my problem.)

Ed

On Mon, 18 Mar 2002, Michael Boudreau wrote:

> I'm having a problem with a file upload when the "read" function is in an
> embperl fragment called by the "Execute" method.
>
> Here's a simplified version of my main file:
>
> --------------------------------------------
> [$ if $fdat{choice} eq "choice1" $]
>
> [- Execute("frag1.epl"); -]
>
> [$ elsif $fdat{choice} eq "choice2" $]
>
> [- Execute("frag2.epl"); -]
>
> [$ endif $]
> --------------------------------------------
>
> The file "frag1.epl" contains this:
>
> [.- if ( $fdat{UploadFile} ) {
>
> open (FILE, "> tmpfile.txt") or die "can't open: $!";
> print FILE $buffer while read($fdat{UploadFile}, $buffer, 32768);
> close (FILE) or die "can't close: $!";
> }
> -]
>
> When I process the main file, I get a zero-length "tmpfile.txt", because
> there is apparently nothing for the "read" function to read.
>
> However, if I move the "read" code out of frag1.epl and into the main file
> (just before the "Execute" statement), the upload works.
>
> Any idea why this is happening?
>
> =================================================
> Michael Boudreau
> Senior Electronic Publishing Developer
> The University of Chicago Press
> 1427 E. 60th Street
> Chicago, IL 60637-2954
>
> phone: 773 753 3298
> fax: 773 753 3383
> =================================================
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail: embperl-help@perl.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
That's not it. I'm able to access all the values of the %fdat hash from
code within the fragment called by "Execute".

In fact, I can make the fragment display the name of the file uploaded from
my desktop, but for some reason embperl can't "read" it.


At 3/18/02 12:58 PM, Ed Grimm wrote:
>frag1.epl runs in a different namespace, where %fdat does not exist.
>If you set EMBPERL_PACKAGE, it supposedly runs everything in that
>namespace, but I've not yet gotten flawless results with this (I'm doing
>a bunch of non-standard stuff; this could be my problem.)
>
>Ed
>
>On Mon, 18 Mar 2002, Michael Boudreau wrote:
>
> > I'm having a problem with a file upload when the "read" function is in an
> > embperl fragment called by the "Execute" method.
> >
> > Here's a simplified version of my main file:
> >
> > --------------------------------------------
> > [$ if $fdat{choice} eq "choice1" $]
> >
> > [- Execute("frag1.epl"); -]
> >
> > [$ elsif $fdat{choice} eq "choice2" $]
> >
> > [- Execute("frag2.epl"); -]
> >
> > [$ endif $]
> > --------------------------------------------
> >
> > The file "frag1.epl" contains this:
> >
> > [.- if ( $fdat{UploadFile} ) {
> >
> > open (FILE, "> tmpfile.txt") or die "can't open: $!";
> > print FILE $buffer while read($fdat{UploadFile}, $buffer, 32768);
> > close (FILE) or die "can't close: $!";
> > }
> > -]
> >
> > When I process the main file, I get a zero-length "tmpfile.txt", because
> > there is apparently nothing for the "read" function to read.
> >
> > However, if I move the "read" code out of frag1.epl and into the main file
> > (just before the "Execute" statement), the upload works.
> >
> > Any idea why this is happening?
> >
> > =================================================
> > Michael Boudreau
> > Senior Electronic Publishing Developer
> > The University of Chicago Press
> > 1427 E. 60th Street
> > Chicago, IL 60637-2954
> >
> > phone: 773 753 3298
> > fax: 773 753 3383
> > =================================================
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> > For additional commands, e-mail: embperl-help@perl.apache.org
> >
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
>For additional commands, e-mail: embperl-help@perl.apache.org


=================================================
Michael Boudreau
Senior Electronic Publishing Developer
The University of Chicago Press
1427 E. 60th Street
Chicago, IL 60637-2954

phone: 773 753 3298
fax: 773 753 3383
=================================================


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
> I'm having a problem with a file upload when the "read" function is in an
> embperl fragment called by the "Execute" method.
>....
>
> When I process the main file, I get a zero-length "tmpfile.txt", because
> there is apparently nothing for the "read" function to read.
>

Which Embperl version do you are using? If I remeber right this is a bug in
older Embperl versions. 1.3.4 and 2.0b6 should work ok. A workaround is to
call the file which reads the data via



[- Execute({inputfile => "frag1.epl", options =>
HTML::Embperl::optDisableFormData}); -]

Gerald



-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925131
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
> frag1.epl runs in a different namespace, where %fdat does not exist.

No, %fdat (as all other special Embperl variables) are imported into all
Embperl pages, so it's the same regardless in which package you run

Gerald


-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925131
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
At 3/18/02 09:15 PM, Gerald Richter wrote:


> > I'm having a problem with a file upload when the "read" function is in an
> > embperl fragment called by the "Execute" method.
> >....
> >
> > When I process the main file, I get a zero-length "tmpfile.txt", because
> > there is apparently nothing for the "read" function to read.
> >
>
>Which Embperl version do you are using? If I remeber right this is a bug in
>older Embperl versions. 1.3.4 and 2.0b6 should work ok.

version 1.3b7, according to the log


>A workaround is to
>call the file which reads the data via
>
>
>
> [- Execute({inputfile => "frag1.epl", options =>
>HTML::Embperl::optDisableFormData}); -]

But I want to be able to access %fdat values from inside frag1.epl.
Wouldn't this prevent me from doing that?


=================================================
Michael Boudreau
Senior Electronic Publishing Developer
The University of Chicago Press
1427 E. 60th Street
Chicago, IL 60637-2954

phone: 773 753 3298
fax: 773 753 3383
=================================================
Re: file upload [ In reply to ]
>version 1.3b7, according to the log

Oh, this is really old. You should upgrade to 1.3.4 ...

>But I want to be able to access %fdat values from inside frag1.epl.
Wouldn't this prevent me from doing that?

You can still access the data in %fdat, it's just not resetup (which doesn't
work) when the file is Executed

Gerald
-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925131
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
>
> I have 1.3.4 installed now, but it still appears that $fdat{UploadFile} is
> not accessible to the "read" function inside my code called by "Execute".
> However, if I set $UploadFile=$fdat{UploadFile}, then I can "read"
$UploadFile.
>

Then my guess that upgrading to 1.3.4 helps was wrong, but anyway it really
makes sense to upgrade.

CGI.pm uses some Perl magic to tie a filehandle and the filename to the same
variable (i.e.$fdat{UploadFile}), sometimes Perl doesn't handle this sort of
magic quite correct. Assigning it to a intermediate variable, as you done,
helps most the time. So I suggest, stay with your solution.


> This option
>
> [- Execute({inputfile => "frag1.epl", options =>
> HTML::Embperl::optDisableFormData}); -]
>
> doesn't work at all.
>

What mean's doesn't work at all? Do you get an error message, if yes what or
doesn't it change anything?

(If you upgraded to 1.3.4 it is not supposed to change anything, that was
just for the older versions)

Gerald

P.S. Please keep the thread on the list


-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925131
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
> M_TWO
> ============
> Apache/1.3.34 (Unix)
> Embperl/1.3.6
> mod_perl/1.29
> mod_ssl/2.8.25
> OpenSSL/0.9.7d
> HTML::Embperl 1.3.6
> CGI 3.04
>
>
> What happens when run on M_TWO is that nothing is written to the $content
> variable.

Embperl 1.3.6 has a bug concerning File upload. You can try to recompile
Embperl with the following patch, which should fix your issues:
ftp://ftp.debian.org/debian/pool/main/libh/libhtml-embperl-perl/libhtml-embperl-perl_1.3.6-2.diff.gz

--
---> Dirk Jagdmann ^ doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
Shoot! Current circumstances might not allow for a recompiling of
Embperl. Does anyone know of any possible work around?

Thanks Dirk.

--Todd White


On Thu, 25 May 2006, Dirk Jagdmann wrote:

> > M_TWO
> > ============
> > Apache/1.3.34 (Unix)
> > Embperl/1.3.6
> > mod_perl/1.29
> > mod_ssl/2.8.25
> > OpenSSL/0.9.7d
> > HTML::Embperl 1.3.6
> > CGI 3.04
> >
> >
> > What happens when run on M_TWO is that nothing is written to the $content
> > variable.
>
> Embperl 1.3.6 has a bug concerning File upload. You can try to recompile
> Embperl with the following patch, which should fix your issues:
> ftp://ftp.debian.org/debian/pool/main/libh/libhtml-embperl-perl/libhtml-embperl-perl_1.3.6-2.diff.gz
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: file upload [ In reply to ]
On 5/25/06, Todd White <tmwhite@merit.edu> wrote:
>
> Shoot! Current circumstances might not allow for a recompiling of
> Embperl. Does anyone know of any possible work around?
>

The problem comes from a change in behaviour of CGI.pm in versions 3.01+

So you could downgrade CGI.pm. Alternatively, the relevant part of the
patch is just stashing the $cgi object somewhere for the life of the
request. You could patch Embperl.pm and put it somewhere earlier in your
@INC path, or redefine just this function in one of your apache startup
scripts, etc.

--- libhtml-embperl-perl-1.3.6.orig/Embperl.pm
+++ libhtml-embperl-perl-1.3.6/Embperl.pm
@@ -1,4 +1,3 @@
-
###################################################################################
#
# Embperl - Copyright (c) 1997-2001 Gerald Richter / ECOS
@@ -935,7 +934,11 @@
else
{
@ffld = $cgi->param;
-
+
+ # CGI.pm 3.01+ removes files on DESTROY, so stash
+ # the CGI object somewhere
+ $$req{'___CGI___'} = $cgi;
+
my $params ;
foreach ( @ffld )
{


--
- Gus