http://www.perlmonks.org?node_id=251788

sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

This might seem odd to some of you but I'm having an unusual problem, it's not the first time this happened either. My script runs nearly perfect right now..it uploads ONLY image formats onto my server, on the other hand if the file attempted to upload is not the correct format no errors are being displayed. (even though there aren't errors, the file isn't uploaded..so that's good). You will notice throughout my script I commented out all or dies because the stop the script from posting any data other than the form to the screen.

Does anyone have any idea why or die would be affecting my script even if it isn't called?

#!/usr/bin/perl -w open( STDERR, ">>/home/sulfericacid/public_html/test/error.log" ) or die "Cannot open error log, weird...an error opening an error log +: $!"; use warnings; use CGI qw/:standard/; use POSIX; my $mode = 0755; print header, start_html('upload form'); print "Please only upload image formats.<br>"; print start_form( -method => post, -enctype => 'multipart/form-data' ), table( Tr( td("File: "), td( filefield( -name => 'upload', -size => 50, -maxlength => 80 ), ), ), Tr( td(), td( submit( 'button', 'submit' ), ) ) ), end_form(), hr; if ( param() ) { # take form data my $remotefile = param('upload'); # make new variable to prevent overwriting of form data my $filename = $remotefile; # remove all directories in the file name path $filename =~ s/^.*[\\\/]//; # full file path to upload directory (must include filename) my $localfile = "/home/sulfericacid/public_html/upload/files/$file +name"; # full url to upload directory (cannot include filename or an end +slash /) my $url = "http://sulfericacid.perlmonk.org/upload/file"; my $type = uploadInfo($remotefile)->{'Content-Type'}; unless ( $type eq 'image/gif' ) { die "image files only!"; } # open a new file and transfer bit by bit from what's in the buffe +r open( SAVED, ">>$localfile" ); # || die $!; while ( $bytesread = read( $remotefile, $buffer, 1024 ) ) { print SAVED $buffer; } close SAVED; chmod $mode, "$localfile"; # or die "can't chmod: $!"; print qq(File was uploaded to <a href="http://sulfericacid.perlmonk.org/uplo +ad/files/$filename">http://sulfericacid.perlmonk.org/upload/files/$fi +lename</a>); # required since module was not preinstalled on server use lib "/home/sulfericacid/public_html/lib/"; use Image::Info qw(image_info dim); # assigning info to a filename (better be an image) my $info = image_info("/home/sulfericacid/public_html/upload/files/$filenam +e"); # if for any reason we can't open the file, this error trap should +pick it up if ( my $error = $info->{error} ) { die "Can't parse image info: $error\n"; } # unommit next line if you want to use/post the image's color #my $color = $info->{color_type}; # declaring the width and heighth of your image my ( $w, $h ) = dim($info); print "<br>"; print qq(&lt;p style ="background:url($url/$filename)\;width:$w\;height:$h\; +"&gt;); }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Or die killing my script
by PodMaster (Abbot) on Apr 20, 2003 at 10:17 UTC
    Does anyone have any idea why or die would be affecting my script even if it isn't called?
    "or die" cannot affect your script if its not being executed.

    Why do you have

    open( STDERR, ">>/home/sulfericacid/public_html/test/error.log" ) or die "Cannot open error log, weird...an error opening an error log +: $!";
    at the top? Also, why do you sprinkle use lines sporadically around your program? (group them together so they're easy to find).

    I see you have written

    open( SAVED, ">>$localfile" ); # || die $!;
    That is never good. If you're going to do stuff like that, do it like
    if( open SAVED, ">>$localfile" ) { print SAVED $crack; }
    Also, why don't you use CGI::Carp; like it's been suggested often to you.

    update: I also forgot to ask you (like I did in the CB a few days ago) why do you use warnings and the -w switch? The only reason I could think of is if you don't know the difference, in which case, you should learn the difference.

    And oh yea, why aren't you use-ing strict? I can't see why people would continue to help you after you continually ignore the same vital advice time and time again (madness).

    update: another question, why print "<br>" if you use CGI ':standard';?


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      why print "<br>" if you use CGI ':standard';?

      Maybe because CGI.pm doesn't have a br method. Or perhaps because the examples in CGI.pm's documentation print "<br>" too.

      Or maybe just because it works, or because it would be more efficient than the method - if it existed.

      Juerd
      - http://juerd.nl/
      - spamcollector_perlmonks@juerd.nl (do not use).
      

        First off,
        CGI.pm defines general HTML shortcut methods for most, if not all of the HTML 3 and HTML 4 tags. HTML shortcuts are named after a single HTML element and return a fragment of HTML text that you can then print or manipulate as you like. Each shortcut returns a fragment of HTML code that you can append to a string, save to a file, or, most commonly, print out so that it displays in the browser window.
        ...
        :standard
        Import ``standard'' features, 'html2', 'html3', 'html4', 'form' and 'cgi'.
        Secondly,
        >perl -MCGI=:standard -e die+br <br /> at -e line 1. >
        Or maybe just because it works, or because it would be more efficient than the method ...
        In that case why does he bother to import it? Why does he bother with any of the html shortcuts, like hr (heredocs anyone)? I say, if you're going to go, go all out, or don't bother.


        MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
        I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
        ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Or die killing my script
by markjugg (Curate) on Apr 20, 2003 at 15:59 UTC

    A couple comments: You may find using CGI.pm built-in "upload" routine easier to use. I do.

    You may also be interested in using Data::FormValidator::Upload to solve your problem. It was designed to handle uploading files, especially images on the web. If the module is not of interest, the code might be. :) This link may break, but I hope to get the module onto CPAN once I work out a few kinks.

    The module will help to validate the file size, file format, and image dimensions, as well as giving you access to these values that it discovers.

    -mark