返回列表 回复 发帖

用 perl 实现文件上传

作者:keendom
email: keendom@sina.com
日期:00-6-29 13:28:47

示例的 HTML 文件如下:
  1. <html>
  2. <body>
  3. <form method="POST" action="psupload.cgi" ENCTYPE="multipart/form-data">
  4. File 1:
  5. <input type="file" name="FILE1">
  6. <br>
  7. File 2:
  8. <input type="file" name="FILE2">
  9. <br>
  10. <input type="submit" value="Upload!">
  11. </form>
  12. </body>
  13. </html>
复制代码
后台的 Perl 程序如下:
  1. &#35;!/usr/bin/perl
  2. &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  3. &#35;&#35; Perl Services Upload Helper v1.0 &#35;&#35;
  4. &#35;&#35; http://www.perlservices.com &#35;&#35;
  5. &#35;&#35; perlservices@perlservices.com &#35;&#35;
  6. &#35;&#35; &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  7. &#35;&#35; You should carefully read all of the following terms and conditions &#35;&#35;
  8. &#35;&#35; before using this program. Your use of this software indicates your &#35;&#35;
  9. &#35;&#35; acceptance of this license agreement and warranty. &#35;&#35;
  10. &#35;&#35; This program is being distributed as freeware. It may be used &#35;&#35;
  11. &#35;&#35; free of charge, but not modified below the line specified. This copyright &#35;&#35;
  12. &#35;&#35; must remain intact. &#35;&#35;
  13. &#35;&#35; &#35;&#35;
  14. &#35;&#35; By using this program you agree to indemnify Perl Services from any &#35;&#35;
  15. &#35;&#35; liability. &#35;&#35;
  16. &#35;&#35; &#35;&#35;
  17. &#35;&#35; Selling the code for this program without prior written consent is &#35;&#35;
  18. &#35;&#35; expressly forbidden. Obtain permission before redistributing this &#35;&#35;
  19. &#35;&#35; program over the Internet or in any other medium. In all cases the &#35;&#35;
  20. &#35;&#35; copyright must remain intact. &#35;&#35;
  21. &#35;&#35; &#35;&#35;
  22. &#35;&#35; There are security hazards involved with this script. Read the readme file&#35;&#35;
  23. &#35;&#35; before using the script. &#35;&#35;
  24. &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  25. &#35;&#35;
  26. &#35;&#35; Start setting up options here:
  27. &#35;&#35; Your path to where you want your files uploaded.
  28. &#35;&#35; Note: NO trailing slash
  29. &#36;basedir = "/home/path/to/directory";
  30. &#35;&#35; Do you wish to allow all file types? yes/no (no capital letters)
  31. &#36;allowall = "yes";
  32. &#35;&#35; If the above = "no"; then which is the only extention to allow?
  33. &#35;&#35; Remember to have the LAST 4 characters i.e. .ext
  34. &#36;theext = ".gif";
  35. &#35;&#35; The page you wish it to forward to when done:
  36. &#35;&#35; I.E. http://www.mydomainname.com/thankyou.html
  37. &#36;donepage = "http://www.perlservices.com/";
  38. &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  39. &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  40. &#35;&#35; DO NOT EDIT OR COPY BELOW THIS LINE &#35;&#35;
  41. &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  42. &#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;&#35;
  43. use CGI;
  44. &#36;onnum = 1;
  45. while (&#36;onnum != 11) {
  46. my &#36;req = new CGI;
  47. my &#36;file = &#36;req->param("FILE&#36;onnum");
  48. if (&#36;file ne "") {
  49. my &#36;fileName = &#36;file;
  50. &#36;fileName =~ s!^.*(\\|\/)!!;
  51. &#36;newmain = &#36;fileName;
  52. if (&#36;allowall ne "yes") {
  53. if (lc(substr(&#36;newmain,length(&#36;newmain) - 4,4)) ne &#36;theext){
  54. &#36;filenotgood = "yes";
  55. }
  56. }
  57. if (&#36;filenotgood ne "yes") {
  58. open (OUTFILE, ">&#36;basedir/&#36;fileName");
  59. print "&#36;basedir/&#36;fileName<br>";
  60. while (my &#36;bytesread = read(&#36;file, my &#36;buffer, 1024)) {
  61. print OUTFILE &#36;buffer;
  62. }
  63. close (OUTFILE);
  64. }
  65. }
  66. &#36;onnum++;
  67. }
  68. print "Content-type: text/html\n";
  69. print "Location:&#36;donepage\n\n";
复制代码

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表