### Filename: file_upload_sys.data
## subs in file:
#hook
$| = 1;
$q = new CGI (\&hook, $sess_ref); # CGI->new(\&hook, $sess_ref); ## Tried it with both of these, neither worked
# Also tried both of these:
#$q = new CGI (\&hook, $sess_ref, $use_tempfile); # CGI->new(\&hook, $sess_ref, $use_tempfile); # Neither worked
$_sloc = '/tmp/s';
# This is the file upload hook, where we can update our session
# file with the dirty details of how the upload is going.
sub hook {
my ($filename,$buffer,$bytes_read,$file) = @_;
my ($sessid) = $ENV{QUERY_STRING};
my $length = $ENV{'CONTENT_LENGTH'};
my $percent = 0;
if ($length > 0) { # Don't divide by zero.
$percent = sprintf("%.1f",
(( $bytes_read / $length ) * 100)
);
}
if($file) {
$file->attr("_uploadUpdateStatus_",join(':',$bytes_read,$length,$percent));
}
# Write this data to the session file.
open (SES, ">$_sloc/$sessid.session");
print SES "$bytes_read:$length:$percent";
close (SES);
}
my $action = $q->param("view");
if ($action eq "upl") {
# Make a file upload hook.
# They are first submitting the file. This code doesn't really run much
# until AFTER the file is completely uploaded.
my $sessid = $q->param("sessid") || $ENV{QUERY_STRING};
open (SES,">$_sloc/$sessid.session") or die "Could not locate file: $! $_sloc/$sessid.session;";
print SES "1:1:1";
close (SES);
my $filename = $q->param("incoming");
my $handle = $q->upload("incoming");
$filename =~ s/(?:\\|\/)([^\\\/]+)$/$1/g;
# Copy the file to its final location.
open (FILE, ">/home$_website_username/www/Videos/$filename") or die "Can't create file: $!";
my $buffer;
while (read($handle,$buffer,2048)) {
print FILE $buffer;
}
close (FILE);
# Delete the session file.
unlink("$_sloc/$sessid.session");
# Done.
$_tfile = "/home$_website_username/www/Videos/" . $filename;
$type = uploadInfo($filename)->{'Content-Type'};
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_tfile);
if(-e "/home$_website_username/www/Videos/$filename") {
$sess_ref->attr("show_message","success|I have successfully uploaded $filename to: http://www.domain.com/Videos/$filename
File type: $type
File size: $size bytes
$_page_content
");
window_redirect("$_url?page=$in{page}&do=$in{do}$sess_id");
} else {
$sess_ref->attr("show_message","error|I could not upload the file: $filename to: http://www.domain.com/Videos/$filename
File type: $type
File size: $size bytes
$_page_content");
window_redirect("$_url?page=$in{page}&do=$in{do}$sess_id");
}
} elsif ($action eq "ping") {
# Now the meat of the CGI script.
print "Content-Type: text/html\n\n";
# Checking up on the status of the upload.
my $sessid = $q->param("sessid");
# Exists?
if (open(OUTF, "$_sloc/$sessid.session")) {
my $data = ;
close (OUTF);
print "$data";
} elsif($sess_ref) {
$data = $sess_ref->attr("_uploadUpdateStatus_");
print "$data";
} else {
print "Error reading Upload progress...$sessid $! $_sloc/$sessid.session";
}
if($sess_ref) {
$sess_ref->close();
}
if($dbh) {
$dbh->disconnect();
}
exit;
} else {
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 );
$_newSessId = $_un . join("", @chars[ map { rand @chars } ( 1 .. 10 ) ]);
$_page_content .= qq~
~ . q~
~;
}
1;