Here's one for all you perl wizards
David Purucker
purucker at hotmail.com
Tue Jun 17 06:43:54 EDT 2003
>I'm trying to do a file upload with PERL and CGI. I have a multipart form
>that adds a file and it uploads. The uploaded file (from a windows PC) both
>form IE and Mozilla, had extra blank lines in it. In my origional testing I
>was uploading BMPs, and it was working fine. Apparently bmps don't mind
>extra whitespace, but Jpgs and PDFs are all messed up. If I open the
>binary file in vi and strip the blank lines the file is fine. Any ideas?
I am not really a PERL wizard, but here goes...
The problem is that, some data is appended to the stream, including form
feeds.
Below is a simple solution, a bit crude... but it works for me.
Hopefully, you can use it, or gain some insight from it.
----------------------------Code
Begin---------------------------------------
#!/usr/bin/perl
use CGI;
$CGI::POST_MAX=2048;
print "content-type: text/html\n\n";
binmode(STDIN);
@upload=<STDIN>; # Loads file into array for
filtering fun.
($d1,$data,$d2)=splice(@upload,0,4); # Strips off header
splice(@upload,$#upload-4,5); # Strips off trailing annoyance
($d1,$d2,$filename)=split(/;/,$data);
$filename=~s/.*filename="(.*)".*/$1/;
$in = join("", at upload);
$in = substr($in,0,length($in)-2);;
open(f1,">/pics/$filename"); # Obviously substitute your
upload directory
binmode(f1);
print f1 $in;
close(f1);
print "Upload Complete, Transferred: ".length($in)." bytes\n";
----------------------------Code End---------------------------------------
Enjoy,
Dave
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
More information about the nflug
mailing list