This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 21st, 2012  |  syntax: Perl  |  size: 1.71 KB  |  views: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl
  2. # upload file script by stas 27/06/2012
  3. use CGI;
  4. use strict;
  5.  
  6. #change CGI temp folder (default = /usr/tmp)
  7. $CGITempFile::TMPDIRECTORY = "/home/tmp";
  8.  
  9. #upload path (make sure you have write permissions)
  10. my $uploadPath = "./uploads";
  11.  
  12. #The name attribute on <input type="file" name="??" /> tag.
  13. #In other words, the post parameter name which includes the file
  14. my $httpParamName="upfile";
  15.  
  16. #Max file Size (bytes)
  17. my $maxSize = 1 * 1024 * 1024;
  18.  
  19. #Set max post size (bytes) automaticly 10kb bigger then size limit
  20. #$CGI::POST_MAX = $maxSize + 10* 1024;
  21.  
  22. ################################
  23. #                       Main
  24. ################################
  25. my $cgi=new CGI;
  26. my $fileToUpload = $cgi->param($httpParamName);
  27. print $cgi->header;
  28.  
  29. if($fileToUpload){
  30.         upload();
  31. }
  32. else{
  33.         print "Please specify file";
  34. }
  35.  
  36.  
  37. ################################
  38. #               Sub routines
  39. ################################
  40. sub upload
  41. {
  42.         #get filehandle from CGI
  43.         my $upload_filehandle = $cgi->upload($httpParamName) || error($!);
  44.        
  45.         #check max size
  46.         if(-s $upload_filehandle > $maxSize){
  47.                 error("File too big. (size limit: $maxSize bytes)");
  48.         }
  49.        
  50.         # If you're on Windows, you'll need this. Otherwise, it has no effect.
  51.         binmode($upload_filehandle);
  52.        
  53.         #create the new file
  54.         open(UPLOAD,">$uploadPath/".$fileToUpload) || error($!);
  55.        
  56.         # If you're on Windows, you'll need this. Otherwise, it has no effect.
  57.         binmode(UPLOAD);
  58.        
  59.         #write the file
  60.         my ($nBytes,$totBytes,$buffer);
  61.         while ( $nBytes = read($upload_filehandle, $buffer, 1024) )
  62.         {
  63.                 print UPLOAD $buffer;
  64.                 $totBytes += $nBytes;
  65.         }
  66.        
  67.         print "uploaded successfully<br/>$fileToUpload ($totBytes bytes)";
  68. }
  69.  
  70.  
  71. sub error
  72. {
  73.          print "Error: $_[0]<br/>";
  74.          exit;
  75. }
clone this paste RAW Paste Data