We help IT Professionals succeed at work.

Upload script help $q->upload(); help

andrea1978
andrea1978 asked
on
177 Views
Last Modified: 2013-12-25
I am trying to create an upload script. It works up until the point of

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

When I tested it to make sure I had all the correct var's
I did a print out of all the vars, I get all the vars except the file handler $fh.
The problem is comming from my $fh = $q->upload($file);

I tested it with.
my $fh = $q->upload($file) || error( $q, "No fh entered." );
 and got no fh entered, why does $fh not get assigned?

Any help on why I am not getting $fh var??

most of the code is down below.

#!/usr/bin/perl -wT
use strict;
use CGI qw/:standard/;
use CGI::Carp   qw/fatalsToBrowser/;
use DBI;

use constant UPLOAD_DIR     => "/Upoads/";
use constant BUFFER_SIZE    => 16_384;
use constant MAX_FILE_SIZE  => 23 * 1_048_576;       # Limit each upload to 1 MB
use constant MAX_DIR_SIZE   => 100 * 1_048_576; # Limit total uploads to 100 MB
use constant MAX_OPEN_TRIES => 100;

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

my $q = new CGI;
my $cgi = new CGI;

my $file = $q->param( "file" );
my $filename  = $q->param( "filename" );
my $fh = $q->upload($file);
my $buffer = "";


print header;

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

$file =~ s/([^\w.-])/_/g;
$file =~ s/^[-.]+//;


# Allow letters, digits, periods, underscores, dashes
# Convert anything else to an 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." );
}

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

print "<BR>";
print "file handler";
print "<BR>";
print $fh;
print "<BR>";
print "file name";
print "<BR>";
print $filename;
print "<BR>";
print "file";
print "<BR>";
print $file;
print "<BR>";

}#end of finial if


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

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: $!";
    while ( readdir DIR ) {
        $dir_size += -s "$dir/$_";
    }
    return $dir_size;
}
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.