469,573 Members | 1,684 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

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('uploadfile');

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($file);
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>starting 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>Untitled Document</title>
</head>
<HTML [5]>
<HEAD></HEAD>
<BODY>
<FORM ACTION="/cgi-bin/upload2.cgi" METHOD="post" ENCTYPE="multipart/form-data">
Image to upload:......
<INPUT TYPE="file" NAME="uploadfile" size="50">
<input type="submit" name="Submit" value="Submit Form">
</BODY>
</HTML>


Thanks And Regards
Prakhar
Jan 18 '07 #1
2 5553
AricC
1,892 Expert 1GB
I moved your post to the Perl forum.
Jan 18 '07 #2
KevinADC
4,059 Expert 2GB
read the CGI documentation:

http://perldoc.perl.org/CGI.html

there are more up to date methods for uploading files and tracking the file size and etc, than the script you posted.
Jan 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Chamomile | last post: by
5 posts views Thread by Dave Navarro | last post: by
reply views Thread by Léopold VALLAURI | last post: by
4 posts views Thread by marfi95 | last post: by
3 posts views Thread by Dean Richardson | last post: by
reply views Thread by Milsnips | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.