Uploading Image(jpg) Files Using CGI- Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prakharv
    New Member
    • Oct 2006
    • 9

    Uploading Image(jpg) Files Using CGI- Problem

    Hi All,
    Below is the code which I am using to upload a jpeg file to the server. But the problem I am facing is that it is not copying the entire contents of the image file to the webserver and it doesn't works on IE.

    Please, help me out as I am new to this and would be highly thankful if anyone has running code for uploading image files to the server using CGI either Perl or C.


    This code i took it from net only.

    #!/usr/bin/perl -w

    use strict;
    use CGI;
    use CGI qw(:standard);


    print "Content-type: text/html\n\n";

    print "Preparing to upload your image. Must be type jpg and LE 1M. ";

    my $query = new CGI;

    print "step 1";
    my $file= $query->param('uploadf ile');

    print $file;

    print "step 2";
    my $info= uploadInfo ($file);

    print "step 3";
    my $type= $info -> {'Content-Type'};
    $file =~ s/.*[\/\\](.*)/$1/;



    if ($file) {


    # open (UPLOAD, ">C:/vps-va/uploadphoto/$file") || Error ();
    open (UPLOAD, "> /usr/webserver/ws031202/web/cgi-bin/$file") || Error ();

    binmode UPLOAD;

    my $info = uploadInfo($fil e);
    my $type= $info->{'Content-Type'};

    print "<p>You want to upload
    <b>$file</b> with a MIME type of
    <b>$type<b>";


    if ($file =~ /\.jpg/ or $file =~ /\.JPG/ ) {

    print "<p>startin g to upload";
    my ($data, $length, $chunk);
    while ($chunk = read ($file, $data, 16384)) {
    print "in loop";
    print UPLOAD $data;

    $length += $chunk;
    print "length is";
    print $length;
    if ($length > 1024000) {
    print "<p> That image is too big. The limit is 1 Meg. I'm deleting.";
    # unlink ("C:/vps-va/upload/$file") || Error2 ();
    unlink ("/usr/webserver/ws031202/web/cgi-bin/$file") || Error2 ();
    exit;
    }
    }
    close (UPLOAD);
    print ("<p> Upload of $file was successful. Size was $length bytes.");

    } else {

    print "<p>Must be jpg. Image not uploaded.";
    unlink ("/usr/webserver/ws031202/web/cgi-bin/$file") || Error2 ();
    }
    } else {
    print "<p> No file was chosen.";
    }

    sub Error {
    print "Error routine 2.1 could not open";
    print "Couldn't open temporary file: $!";
    exit;
    }
    sub Error2 {
    print "Couldn't delete file: $!";
    exit;
    }
    sub Error3 {
    print "Bad Password";
    exit;
    }



    HTML :

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitl ed Document</title>
    </head>
    <HTML [5]>
    <HEAD></HEAD>
    <BODY>
    <FORM ACTION="/cgi-bin/upload2.cgi" METHOD="post" ENCTYPE="multip art/form-data">
    Image to upload:......
    <INPUT TYPE="file" NAME="uploadfil e" size="50">
    <input type="submit" name="Submit" value="Submit Form">
    </BODY>
    </HTML>


    Thanks And Regards
    Prakhar
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    I moved your post to the Perl forum.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      read the CGI documentation:



      there are more up to date methods for uploading files and tracking the file size and etc, than the script you posted.

      Comment

      Working...