By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

Uploading Image(jpg) Files Using CGI- Problem

P: 9
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
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,892
I moved your post to the Perl forum.
Jan 18 '07 #2

KevinADC
Expert 2.5K+
P: 4,059
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.