Hi all,

I've made a script for uploading a file, and it uploads the file but it has
a problem.

I want to print another page that prints "File uploading..." while the file
is uploading.

Unfortunately I can't do that.
After pressing the upload button, the page remains the same. It only appears
"Opening Page..." in the task bar.

Only after the file is uploaded it prints the result page, even a part of
that page is printed before opening and printing the file.

Do you have any idea why?

Thank you.

Here is the script:

#!/usr/bin/perl -WT

=author Teddy;
Script made by Octavian Rasnita [EMAIL PROTECTED]
=cut

use lib "/var/www/teddy/lib";

use strict;
use CGI;

my $q = new CGI;

#The max size of the uploaded file:
$CGI::POST_MAX = 1024 * 1024 * 30;
my $maxfile = $CGI::POST_MAX;

#The folder with the uploaded files:
my $outfolder = "/var/www/teddy/html/up";

my $hostroot = $ENV{'HTTP_HOST'};
#The web address of the folder with uploaded files:
my $web_folder = "http://$hostroot/up";;

#The max size of the directory with uploaded files
my $maxfolder = 1024 * 1024 * 100;

#Max number of retries to rename (incrementing) the file:
my $maxtries = "20";

#Should this script announce  by mail about this upload?
my $sendmail = 1;

#The email address used for announcing this upload:
my $email_address = '[EMAIL PROTECTED]';

#The sender address of this message:
my $email_from = '[EMAIL PROTECTED]';

#The address of the SMTP mail server:
my $server = '127.0.0.1';

#The Hello address:
my $hello = "localhost";

#The path to the sendmail program:
my $mailprog = "/usr/sbin/sendmail";

#The subject of the message:
my $email_subject = 'New file uploaded!';

### End editing ###

my $screen = $q -> param ('screen');
my $script = $ENV{'SCRIPT_NAME'};
my $name = $q -> param ('name');
my $email = $q -> param ('email');
my $comments = $q -> param ('comments');
my $filename;
my $allpath;
my $filesize;
my $host;
### Start code ###

my $dirsize = dir_size($outfolder);
my $content_length = $ENV{'CONTENT_LENGTH'};
if (! $content_length) {
$content_length = 0;
}
my $filled_space = $dirsize + $content_length;

if ($content_length > $maxfile) {
#If the size of the file is bigger than the max size of a file that is
allowed:
my $freespace = $maxfile;
$freespace = int(($freespace / 1048576) *10)/10;

print "Content-type: text/html\n\n";
print <<eof;
<html lang="en"><head><title>File too big!</title></head>
The folder is full and there is not enough space for a file bigger than
$freespace MB.<br>
<br>
If you want to upload a bigger file, please
<a href="mailto:$email_address";>ask me how to do it.</a>
</body></html>
eof

exit;
#End if:
}

if ($filled_space > $maxfolder) {
#If the size of the file + size of folder  is bigger than the max allowed
size of folder:
my $freespace = $maxfolder - $dirsize;
$freespace = int(($freespace / 1048576) *10)/10;

print "Content-type: text/html\n\n";
print <<eof;
<html lang="en"><head><title>File too big!</title></head>
The folder is full and there is not enough space for a file bigger than
$freespace MB.<br>
<br>
If you want to upload a bigger file, please
<a href="mailto:$email_address";>ask me how to do it.</a>
</body></html>
eof

exit;
}

if (! $screen) {
&print_form;
}
elsif($screen eq "1") {
&get_host;
&upload;
if ($sendmail eq "1") {
&send_mail;
}
}

sub print_form {
print "Content-type: text/html\n\n";

my $freespace = $maxfolder - $dirsize;
if ($freespace > $maxfile) {
#Daca e loc mai mult decat marimea maxima a unui fisier permis:
$freespace = $maxfile;
$freespace = int(($freespace / 1048576) *10)/10;
}
else {
#Daca e loc mai putin decat marimea unui fisier:
$freespace = int(($freespace / 1048576) *10)/10;
}

print <<eof;
<html lang="en"><head><title>Upload a file!</title></head><body>
Please make sure the file you want to upload is not bigger than $freespace
MB.<br>
If it is, it won't be saved on the server. It will be lost.<br>
If you want to upload a bigger file than this limit, please <a
href="mailto:$email_address";>ask me how to do it.</a>
<br><br>
You don't need to fill all the fields. You need just to type the correct
path to a file you want to upload.
<br>
You can leave all the other fields blank if you want, but I would recommend
filling them.
<br><br>
If you are using a screen reader, and if it can't read the "Browse" button,
you have to activate the forms mode, then press tab, then space bar.<br>
This will open the "Browse for file" window<br>
<form method="post" enctype="multipart/form-data" action="$script">
<input type="hidden" value="1" name="screen">
File name:<input type="file" name="file" size="70">
Your name: <input type="text" name="name" size="30"><br>
Your email: <input type="text" name="email" size="40"><br>
Comments: <textarea name="comments" rows="4" cols="70"></textarea>
<br>
<input type="submit" value="Upload file">
</form>
</body></html>
eof
#end sub print_form:
}

sub upload {
my ($filen, $ext);

#Print the start of the page:
$| = 1;
print <<eof;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd";>
<html lang="en"> <head> <title>Uploading the file...</title>
<link rel="stylesheet" href="/teddy.css" type="text/css">
</head>
<body>
<div class="center">
<div class="text_h1">
The file is uploading...
</div></div>
<br><br>
eof

my $file = $q->upload('file');

$filename = $file;
$filename =~ s/^.*\///g;
$filename =~ s/^.*\\//g;

$filename =~ s/\-/_/g;
$filename =~ s/^\.*//g;
$filename =~ s/ /_/g;
$allpath = $file;

if ($filename =~ /\./) {
$filen = $`;
$ext = $';
}

for (my $i=1; $i < $maxtries; $i++) {
if (-e "$outfolder/$filename") {
$filename = $filen . "_" . $i . '.' . $ext;
}
}

#Create the file on the server, and print the "." on the page:
open(OUT, ">$outfolder/$filename")
or die "Can't open $outfolder/$file for writing. - $!";
binmode OUT;
binmode $file;
while (read($file, my $buffer, 4096)) {
print OUT $buffer;
#Print to the browser to prevent the timeout:
print ". ";
}
close OUT;

$filesize = -s "$outfolder/$filename";
$filesize = (int(($filesize / 1024) * 10) / 10);
$filesize = "$filesize KB";

#Print on the browser:
print <<eof;
<br><br>
The file $filename was successfully uploaded!<br>
The file has $filesize.<br>
<div class="center">
<a href="$script">Go back if you want to upload one more file.</a>
&nbsp; &nbsp;
<a href="/">Go to main page on Teddy Center!</a>
</body></html>
eof

#End the subroutine
}

sub dir_size {
    my $dir = shift;
    my $dir_size = 0;

    # Loop through files and sum the sizes; doesn't descend down subdirs
opendir DIR, $dir or die "Unable to open $dir: $!";
foreach ( readdir DIR ) {
$dir_size += -s "$dir/$_";
    }
    return $dir_size;
}

sub send_mail {
#Check the OS:
if ($^O =~ /MSWin/i) {
#It is Windows:
use Net::SMTP;
my $m = Net::SMTP -> new($server,
-hello => $hello,
-debug => 0,
-timeout => 30);

$m -> mail($email_from);
$m -> to($email_address);
$m -> data();
$m -> datasend(
"To: $email_address
From: $email_from
Subject: $email_subject

There is a new file uploaded by $name: $email
The address of this file is:
$web_folder/$filename
The size of the file is: $filesize
$comments
The path used for uploading this file is: $allpath
The host name is: $host
");
$m -> dataend;

#End if (windows):
}
else {
#It is Unix:
#Delete the environment variables for using -T with no problems:

BEGIN {
    $ENV{PATH} = "/bin:/usr/bin";
    delete @ENV{ qw( IFS CDPATH ENV BASH_ENV ) };
}

open (MAIL, "|$mailprog -t -i")
        or die "Could not open sendmail: $!";

print MAIL <<eom;
From: $email_from
To: $email_address
Subject: $email_subject

There is a new file uploaded by $name, $email.
The  address of the uploaded file is:
$web_folder/$filename
The size of the file is: $filesize
$comments
The path used for uploading this file was: $allpath
The host name is: $host
eom

close MAIL or die "Error closing sendmail: $!";

#End else (unix):
}

#End sub send_mail:
}


sub get_host {
 my ($ip_address,$ip_number,@numbers);
 if ($ENV{'REMOTE_HOST'}) {
  $host = $ENV{'REMOTE_HOST'};
 }
 else {
  $ip_address = $ENV{'REMOTE_ADDR'};
  @numbers = split(/\./, $ip_address);
  $ip_number = pack("C4", @numbers);
  $host = (gethostbyaddr($ip_number, 2))[0];
 }
 if ($host eq "") {
  $host = "$ENV{'REMOTE_ADDR'}";
 }
 else {
  $host = $host;
 }
#end sub get_host
}



Teddy Center: http://teddy.fcc.ro/
Mail: [EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to