471,850 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

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

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 ();
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: $!";
sub Error2 {
print "Couldn't delete file: $!";
sub Error3 {
print "Bad Password";


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<HTML [5]>
<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">

Thanks And Regards
Jan 18 '07 #1
2 5778
1,892 Expert 1GB
I moved your post to the Perl forum.
Jan 18 '07 #2
4,059 Expert 2GB
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.
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 NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.