in reply to Can I do a file upload without using any modules?
frinky, the limitation that you have run into is one of the many reasons why people recommend that you use CGI.pm or one of its (few) competitors on the CPAN. Your code cannot handle file uploads and is unlikely to be able to do so at any point in the future. This is because the encoding for 'multipart/form-data' is much different from the standard encoding that you are used to. Lets look at a standard upload form.
<form action="/cgi-bin/display.cgi" method="post" enctype="multipart/f +orm-data"> <input type="file" name="file" /><br /> <input type="hidden" name="hiddenData" value="nothing to see h +ere, folks" /><br /> <input type="checkbox" name="someCheckbox" value="someValue" check +ed="checked" />Checkbox<br /> <input type="submit"><br /> </form>
Let's say that I use that form to upload a small file named 'temp.pl'. The entity-body (the part after the headers) that is sent to the browser might resemble the following:
-----------------------------7d03ca41c0 Content-Disposition: form-data; name="file"; filename="C:\WINDOWS\Desk +top\temp.pl" Content-Type: text/plain #!C:/perl/bin/perl.exe -w use strict; $SIG{INT} = 'IGNORE'; my $count=0; while (){ print $count++; } -----------------------------7d03ca41c0 Content-Disposition: form-data; name="hiddenData" nothing to see here, folks -----------------------------7d03ca41c0 Content-Disposition: form-data; name="someCheckbox" someValue -----------------------------7d03ca41c0--
Not only is that rather difficult to parse, but different browsers often have subtly different formats for multipart/form-data. Debugging those issues can be a nightmare. That is one of the many reasons people suggest that you use a standard module instead of trying to roll your own.
Since CGI.pm is part of the standard distribution, you should have no problem using it. Check out Lesson 2 of my CGI course for more information about this topic.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|