Hi,
I have been studying with the O'Reilly book "CGI Programming with Perl" -
Chapter 5 page 99
Below is an example of a upload.cgi that utilizes CGI.pm
I am not able to get it to work correctly yet and I am trying to find out if
it is the directory I am trying to load the file to or a progammatical
error. The html is at:
http://www.vargasmedia.com/upload.htm
I have been using
http://www.vargasmedia.com/cgi-bin/env_var.cgi
to help me get the information I need to upload files using the correct
path.
Is that a good way to go about it and if so what Environment Variable should
I be looking at to return a path I can use...
DOCUMENT_ROOT
I have an empty folder on my server under www that is called "uploads" and
wanted to upload files there.
Here is the code from the Book "CGI Programming with Perl - it is in Chapter
5 - page 99
-PS:btw- What is the CHMOD setting that I should be using with this CGI and
"uploads" directory?
Forgive me I'm new :)
Any help extremely appreciated.
Steve
############################################################################
#########
#!/usr/bin/perl -wT

use strict;
use CGI;
use Fcntl qw( :DEFAULT :flock );

# use constant UPLOAD_DIR => "/usr/local/bin";
use constant UPLOAD_DIR  => " /www/vargasmedia/uploads/";
use constant BUFFER_SIZE =>16_384;
use constant MAX_FILE_SIZE => 100 * 1_048_576;
use constant MAX_DIR_SIZE => 100 * 1_048_576;
use constant MAX_OPEN_TRIES =>100;

$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 $file       = $q->param( "file" )        || error( $q, "No file
recieved." );
my $filename   = $q->param( "filename" )    || error( $q, "No filename
entered." );
my $fh         = $q->upload( $file );
my $buffer     = "";

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

# Convert odd ball characters to underscore
$filename =~ s/[^\w.-]/_/g;
if ( $filename =~ /^(\w[\w.-]*)/ ) {
     $filename = $1;
}
else {
     error( $q, "Invalid file name; files must start with a letter or
number." );
}

# open output file, making sure the file name is unique
until ( sysopen OUTPUT, UPLOAD_DIR . $filename, O_CREAT | O_EXCL ) {
    $filename =~ s/(\d*)(\.\w+)$/($1||0) + 1 . $2/e;
    $1 >= MAX_OPEN_TRIES and error( $q, "Unable to save your file." );
}
# This is neccessary for non-Unix systems does nothing on UNIX
binmode $fh;
binmode OUTPUT;

# write contents to output file
while ( read($fh, $buffer, BUFFER_SIZE ) ) {
   print OUTPUT $buffer;
}

close OUTPUT;

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


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

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",
"occured: "),
        $q->p( $q->i( $reason )),
        $q->end_html;
     exit;
}

































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

Reply via email to