Nick
Nick

Reputation: 7

Perl Upload Code Leaves Blank Upload File

I have scoured the internet for code that can be used to allow users of my website to upload photos. I have tried a number of open source Perl codes, all with the same result: the new file uploaded to my server is blank!

Here is the code:

First, a webpage that asks for a file:

<form name="input" action="/cgi-bin/upload.pl" method="get" ENCTYPE="multipart/form-data">
Upload Photo:<input type="file" name="pic" /><BR>
<input type="submit" name="Submit" value="Submit Form" />

Now for the upload code:

#!/usr/bin/perl 
use CGI;
my $cgi = new CGI;
my $dir = "/home/mydomain/www/wwwboard/uploads";
my $file = $cgi->param('pic');
my $filename = $file;
$filename =~ s/^.*\\//;
$filename =~ s/^.*\///;
$filename =~ s/\s /_/g;

open(LOCAL, ">", "$dir/$filename") or die $!;
while(<$file>) {
    binmode LOCAL;
print LOCAL $_;
}
close(LOCAL);
print "Content-type: text/html\n\n";
print "$file has been successfully uploaded... thank you.\n";

I'm not a Perl expert, but it seems to me the problem is that the variable $file = $cgi->param('pic'); is only picking up the basename of the file-handle from the user's computer. For example, when I try to upload a file called "/home/nick/Pictures/photo.JPG", the message I get from the server says "photo.JPG has been successfully uploaded... thank you.". Maybe that's not the problem, I don't know...

I get the same result using firefox and chrome. The permissions for the uploads folder is 777.

I know the security on this code is weak. I can get to that later. right now I just want to see it work.

Upvotes: 0

Views: 2382

Answers (1)

Brian Roach
Brian Roach

Reputation: 76898

If you google perl cgi file upload the first link is to a tutorial that shows you exactly how to do this: http://www.sitepoint.com/uploading-files-cgi-perl/

  • You're doing a get instead of a post on your form
  • You're not reading the file from the CGI object, which is accessed via $cgi->upload('pic')

Upvotes: 2

Related Questions