### Filename: file_upload_sys.data
## subs in file:
#hook
$q = new CGI (\&hook);
# Set global value for session id (always members username)
$sessid = $_un;
# 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) = @_;
# Calculate the (rough estimation) of the file size. This isn't
# accurate because the CONTENT_LENGTH includes not only the file's
# contents, but also the length of all the other form fields as well,
# so it's bound to be at least a few bytes larger than the file size.
# This obviously doesn't work out well if you want progress bars on
# a per-file basis, if uploading many files. This proof-of-concept only
# supports a single file anyway.
my $length = $ENV{'CONTENT_LENGTH'};
my $percent = 0;
if ($length > 0) { # Don't divide by zero.
$percent = sprintf("%.1f",
(( $bytes_read / $length ) * 100)
);
}
# Write this data to the session file.
open (SES, ">./$sessid.session");
print SES "$bytes_read:$length:$percent";
close (SES);
}
$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.
$filename = $q->param("incoming");
$handle = $q->upload("incoming");
$filename =~ s/(?:\\|\/)([^\\\/]+)$/$1/g;
# Copy the file to its final location.
open (FILE, ">/home/path/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("./$sessid.session");
# Done.
$_tfile = "/home/path/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/path/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
");
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.
# Exists?
if (-f "./$sessid.session") {
# Read it.
open (READ, "./$sessid.session");
my $data = ;
close (READ);
print "$data";
} else {
print "Error reading Upload progress...";
}
if($sess_ref) {
$sess_ref->close();
}
if($dbh) {
$dbh->disconnect();
}
exit;
} else {
# Write this data to the session file.
$_page_content .= qq~
~ . q~
~;
}
1;