Hello , I've seen a lot of file upload problems on
this list so thought id add my own!
Ive got a upload script that seems to start uploading
the specified file ok, but unless its a text file or
small binary file it seems to timeout. I notice that
the file is sitting in /var/tmp/ under some random
name like CGItemp6342.
And i also notice in the directory it writes to, there
is a file sitting there with 0 size, so it looks like
its starting until the timeout(or something else)
stops the file writing...

I am running redhat 7.1
apache 1.3
using CGI.pm

Any common solutions anyone knows , would be helpful.
thanks
here is the script below

----------------------------------------

use CGI qw( :standard);
use Fcntl qw( :DEFAULT :flock );
use constant UPLOAD_DIR => "/usr2/files/";
use constant BUFFER_SIZE => 16_384; #32_768;
use constant MAX_FILE_SIZE => 10 * 1_048_576; #30 megs
max file size
use constant MAX_DIR_SIZE  => 300 * 1_048_576; # 300
megs max files in dir
use constant MAX_OPEN_TRIES => 1000;

$CGI::DISABLE_UPLOADS = 0;
$CGI::POST_MAX  = MAX_FILE_SIZE;

my $q = new CGI;
$q->cgi_error and error( $q, "Error transferring file:
" . $q->cgi_error );
my $bytesread;
my $file = $q->param( "file") || error($q, "No file
received.");
my $filename = $q->param( "filename") || error($q, "No
file Entered");

#I tried using $fh and got the same response so
#commented it out
#my $fh = $q->upload( "file" );
my $buffer = "";

if (dir_size(UPLOAD_DIR) + $ENV{CONTENT_LENGTH} >
MAX_DIR_SIZE){
        error($q, "Upload Directory is full");
}

#allow letters digits periouds underscores dashes
#otherwise convert to underscore

$filename =~ s/[^\w.-]/_/g;
if( $filename =~ /^(\w[\w.-]*)/ ) {
        $filename = $1;
}
else{
error ($q, "Invalid filename; Files must start with a
letter or number.");
}

#open output file, 
my $filedir = UPLOAD_DIR . $filename;
open (OUTPUT, ">$filedir");
flock( OUTPUT, LOCK_EX ) or error( $q, "Invalid. $!");

#For unix system uploads
#binmode $fh;
binmode $file;
binmode OUTPUT;

while (<$file>){
        print OUTPUT $_;
}
## Commented this part out since i tried using $fh and
#got the same behaviour
#write contents to output file using $fh
#while ($bytesread=read ($fh, $buffer, 1024)) {
#       print(OUTPUT $buffer);
#}

print ( redirect(-location=>'/members_ok.shtml'));
flock( OUTPUT, LOCK_UN ) or die ( "Cannot unlock file:
$!" );
close OUTPUT;

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

#loop thru files sum sizes, dont descend subdirs
opendir (DIR, $dir) or die "Unable to open $dir: $!";
while (readdir DIR){
        $dir_size += -s "$dir$_";
}
return $dir_size;
closedir(DIR);
}

sub error {
        my ($q, $reason) = @_;
        print $q->header("text/html"),
        $q->start_html("Error"),
        $q->h1( "error" ),
        $q->p("Your upload was not processed because the
following error: "),
        $q->p($q->i($reason)),
        $q->end_html();
exit;
}

and the HTML form

--------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<html>
<body>
<span class="Sheader">Upload files</span>
Enter the filepath on your local system, 
</p>

<FORM METHOD="POST" ACTION="/cgi-bin/upload.cgi" 
ENCTYPE="multipart/form-data"><p><p class="highlight">
Local filename: (on your machine)<br>
<input NAME="file" TYPE="FILE">
<p class="highlight">
Remote filename: 
<input name="filename" type="text" size="15">

<br><br>
</FORM>         
</span>
</body>
</html>

__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com

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

Reply via email to