Advertisement
Guest User

Untitled

a guest
Nov 21st, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.71 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement