perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew O'Brien" <andr...@oriel.com.au>
Subject RE: Upload file
Date Thu, 09 Oct 2003 01:27:47 GMT

Firstly Rado, there is a typo in your "open" line: a missing closing ".
The only other thing I would suggest is using BINMODE. See below for an
example routine.

> In EplSite WorkFlow i am using this routine and works fine:
> 
>     use CGI;
>     $query = new CGI;
> 
>     if( $file = $query->param('ImageName') )
>     {
>        @thefile = split(/\\/,$file);
>        $elements = @thefile;
>        $thedocument = "$$-@thefile[$elements-1]";
>        $thedocument =~ s/\s/_/g;
>        if( open (OUTFILE,">$globalp->{Documents_path}/$thedocument") )
>        {
>           binmode(OUTFILE);# Explicit binary mode
>           while ($bytesread=read($fdat{attachthis},$buffer,32768))
>            {
>              print OUTFILE $buffer;
>            }
>           close(OUTFILE);
>        }
>     }
> 
> I am using CGI for some manipulation on filename but you 
> could not use it.

Ouch. CGI is completely unnecessary. Evaluating $fdat{ImageName} in
string context will provide the filename of the uploaded file - Embperl
is using CGI internally already to give you this filehandle. An
alternative version (with the usual off-the-top-of-my-head typo caveats)
that uses somewhat safer variable scope:

 if (my $file = "$fdat{ImageName}") {
   require File::Spec;
   $file = (File::Spec->splitpath($file))[2] # just the filename, system
independently
   $file =~ s/[^\w\.]/_/g;
   $file =~ s/_+/_/g;      # replace unwanted characters, somewhat
aggressively.
   if ( open(OUTFILE, ">$some_path/$file") ) { # this should use
File::Spec as well.
     binmode OUTFILE;
     my $buffer;
     print OUTFILE $buffer
       while read($fdat{ImageName}, $buffer, 32768);
     close OUTFILE;
   }
 }

Cheers,

Andrew

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Mime
View raw message