
Untitled
By: a guest on
Nov 21st, 2012 | syntax:
Perl | size: 1.71 KB | hits: 57 | expires: Never
#!/usr/bin/perl
# upload file script by stas 27/06/2012
use CGI;
use strict;
#change CGI temp folder (default = /usr/tmp)
$CGITempFile::TMPDIRECTORY = "/home/tmp";
#upload path (make sure you have write permissions)
my $uploadPath = "./uploads";
#The name attribute on <input type="file" name="??" /> tag.
#In other words, the post parameter name which includes the file
my $httpParamName="upfile";
#Max file Size (bytes)
my $maxSize = 1 * 1024 * 1024;
#Set max post size (bytes) automaticly 10kb bigger then size limit
#$CGI::POST_MAX = $maxSize + 10* 1024;
################################
# Main
################################
my $cgi=new CGI;
my $fileToUpload = $cgi->param($httpParamName);
print $cgi->header;
if($fileToUpload){
upload();
}
else{
print "Please specify file";
}
################################
# Sub routines
################################
sub upload
{
#get filehandle from CGI
my $upload_filehandle = $cgi->upload($httpParamName) || error($!);
#check max size
if(-s $upload_filehandle > $maxSize){
error("File too big. (size limit: $maxSize bytes)");
}
# If you're on Windows, you'll need this. Otherwise, it has no effect.
binmode($upload_filehandle);
#create the new file
open(UPLOAD,">$uploadPath/".$fileToUpload) || error($!);
# If you're on Windows, you'll need this. Otherwise, it has no effect.
binmode(UPLOAD);
#write the file
my ($nBytes,$totBytes,$buffer);
while ( $nBytes = read($upload_filehandle, $buffer, 1024) )
{
print UPLOAD $buffer;
$totBytes += $nBytes;
}
print "uploaded successfully<br/>$fileToUpload ($totBytes bytes)";
}
sub error
{
print "Error: $_[0]<br/>";
exit;
}