Category: CGI
Author/Contact Info /msg sulfericacid
Description: CGI form letting users, or yourself, to upload images to your server. Images are currently set at gif, jpg/jpeg and bmp files however if you had any reason to you could accept or limit any file type you want by reading the documenation.

Images are chmoded then read by Image::Info to inform you of the image dimensions so it can produce the CSS code you'd need to use the image. You can remove this entire feature but the purpose I had with the script was to allow users who didn't have webspace or html knowledge to upload and use images freely.

Things I've learned:

  • Chmoding files
  • Uploading/restricting files and types
  • use lib
  • #!/usr/bin/perl -w
    
    use warnings;
    use CGI qw/:standard/;
    
    use POSIX;
    
    my $mode = 0755;
    
    print header, start_html('upload form');
    
    print "Upload formats allowed: jpg, gif, bmp.<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;
    
    print "By clicking Submit you are agreeing that any legal discreptanci
    +es involved with the images you upload";
    print " are not the responsibility of the designer of this script or t
    +he webhost.  You are agreeing these";
    print " pictures are not copyright material, do not contain viruses an
    +d does not promote sexual or violent";
    print " activities.  It is a legal signature of awknowledgement once y
    +ou click the Submit button.<br><br>";
    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 = "";
    
        # full url to upload directory (cannot include filename or an end 
    +slash /)
        my $url = "";
    
        my $type = uploadInfo($remotefile)->{'Content-Type'};
        unless ( $type eq 'image/pjpeg' || $type eq 'image/gif' || $type e
    +q 'image/bmp') {
            print "Wrong!  This is not a supported file type.";
            exit;
        }
    # print "type: $type <br><br>";
    
        # 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 "-----------------------------<br>";
        print
    qq(File was uploaded to <a href="$url\/$filename">$url\/$filename</a>)
    +;
    
        # required since module was not preinstalled on server
        # use lib "";
        use Image::Info qw(image_info dim);
      
        # assigning info to a filename (better be an image)
        my $info =
          image_info("$localfile");
       # 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><br><br>";
        print "Image code:<br>";
        print
    qq(&lt;p style =\"background:url\($url\/$filename\)\;width:$w\;height:
    +$h\;\"&gt;);
    print "<br>-----------------------------<br>";
    }
    
    Replies are listed 'Best First'.
    Re: Image uploader
    by Anonymous Monk on Apr 22, 2003 at 07:52 UTC
      -- Why aren't you using strict?
        If you're interested in using strict, add:

        use strict; my $buffer; my $bytesread;
        near the top of the script

        "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