Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading files with "use CGI", files corrupt

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm having a heck of a time trying to write a website admin tool, every thing was going well till i tried to add a file uploader!!!

my program works fine in the sense it uploads the correct file, to the correct place with the correct name, even the file size is the same as the original...HOWEVER....

when you try to open the uploaded file it is corrupt.

any help would be great,

here's my code....


#!/usr/bin/perl


use CGI;

$cgi = new CGI;

$ft = $cgi->param('ft');
$file = $cgi->param('fl');
$domain = "$err_url = "$domain/err.htm";
$id = $cgi->param('userid');
$pw = $cgi->param('password');
$fa = $cgi->param('FA');
$type = $cgi->uploadInfo($file)->{'Content-Type'};
$desc = "File Upload";
$cmd = $cgi->param('cmd');

$len = length($file);
$pos = $len-3;
$ext = substr($file,$pos,3);

if($file eq ""){$mess="[Upload failed!] - No file selected.";&error;}
else {

if($ft eq "rm"){$dir = "./tmp/mp3";
if($ext ne ".rm"){$mess="[Upload failed!] - Invalid file type selected.";&error;};
};

if($ft eq "mp3"){$dir = "./tmp/mp3";
if($ext ne "mp3"){$mess="[Upload failed!] - Invalid file type selected.";&error;};
};

if($ft eq "jpg"){$dir = "./tmp/images";
if($ext ne "jpg"){$mess="[Upload failed!] - Invalid file type selected.";&error;};
};

if($ft eq "gif"){$dir = "./tmp/images";
if($ext ne "gif"){$mess="[Upload failed!] - Invalid file type selected.";
}

if($ft eq ""){
if($file ne ""){$mess="[Upload failed!] - No file type selected.";&error;
};
}


if($mess eq &quot;&quot;){$mess=&quot;[Upload Complete!]<br><br><font color=black><b>$file&quot;;}

$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename

$name = $2;
open(LOCAL, &quot;>$dir/$name&quot;) or die $!;
binmode(LOCAL);
while(<$file>) {
print LOCAL $_;
}

&uploaded;

#### error & uploaded subs ---->>>>


Ok although i said the file was going to the right place I also have another question... is it possible to use PERL/CGI to FTP the uploaded file to another one of our servers giving the perl code the correct username & password details.???

cheers,

craig.
 
perldoc CGI

open (OUTFILE,&quot;>>/usr/local/web/users/feedback&quot;);
while ($bytesread=read($filename,$buffer,1024)) {
print OUTFILE $buffer;
}
close (FILE);

Also check options of your webserver.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top