I'm trying to add a file upload feature to some perl code and there seem to be a lot of very different examples on the web, most of them either different from my approach or just way over my head. My HTML code and pages are very basic and provide simple but sufficient functionality. Here is a fragment of my code that should be enough to show what I am trying to do:
#!/usr/bin/perl -w
# code_fragment.pl
use strict;
use CGI qw(:standard escapeHTML);
# ------ start posting to browser ------
print header (),
start_html( (-head => [ Link ({ -rel => 'icon',
-type => 'image/png',
-href => '/perl_img/my_icon_01.ico'})
+, ] ),
(-title => "My File Upload Utility", -bgcolor => "whit
+e") );
print ('<IMG SRC="http://' . $server_ip_addr . '/image004.jpg" /><br /
+>' . "\n");
print ('<h2>UPLOAD A NEW FILE</h2>' . "\n");
# ------ post form to browser ------
print start_multipart_form(-action => url()),
'<input type="hidden" name="MAX_FILE_SIZE" value="5000" />', "\n",
'Select file to upload: ', "\n",
'<input name="uploadedfile" type="file" size="40"/><br />', "\n",
"<br /><br />\n",
"<br /><br />\n",
'<input type="submit" name="choice" value="Upload File" /> &nb
+sp; ', "\n",
'<input type="submit" name="choice" value="Abort Upload" />', "\n",
end_form();
print end_html();
exit(0);
My code puts a working file upload box on the browser and seems to work properly. After the browser returns I have the file name in param("uploadedfile") as expected. My problem is I don't know how to access the file.
Some of the php file-upload examples on the web seem very easy. Apparently in php $_FILES['uploadedfile']['name'] will contain the path and name of the file on the client machine and $_FILES['uploadedfile']['tmp_name'] will contain the path and name of the temporary file that the browser uploaded onto the server after the input type=file executed. Use this to copy the file to the final location and a useful file upload is done.
It would be great if someone could look at my code and let me know if something as simple as the php example can be done in perl. Everything I find goes off into advanced CGI and Apache calls and leaves me feeling like I could spend days and many failures just to do something simple.
Thanks,
Bruce
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.