Uploading Files Using CGI and Perl

I got INTERNAL SERVER ERROR even though I have checked the URLs, Paths, and the permissions. Any other suggesstions on this??

Can you install/run other Perl CGI scripts on your site without problems?

At what point do you get an internal server error?

Also, you could try ELATED’s Troubleshooting CGI Scripts tutorial, which explains how to narrow down internal server error problems.

Cheers,
Matt

If you’re running on a unix box, try running perl -cwT /whatever/the/file’s/path/is - that should tell you any errors. Alternatively, add “use CGI::Carp qw(warningsToBrowser fatalsToBrowser);” underneath the use CGI (without the inverted commas) - this tells you what the error is when you load your page (but it doesn’t show as many errors as the first method) - you’ll want to remove the line before publishing your site though… it could show hackers potential weaknesses…

Great idea to use CGI::Carp, Tom - hadn’t thought of that :slight_smile:

Matt

this page its really good

Thanks very much. A very well written doc.
Should also add that the webserver must have write access to the upload directory.

Hello,

Im looking at useing this on my site but i have a questions.

whats the size limit for the files (i need away so that users can upload files bigger then 2mb in size (IE’s file size limit))

thank you.

AFAIK IE doesn’t have an upload file size limit.

To set a limit server-side, use CGI.pm’s $CGI::POST_MAX variable. See http://search.cpan.org/~lds/CGI.pm-3.11/CGI.pm#Avoiding_Denial_of_Service_Attacks for details. I believe the default behaviour is to have no limit.

Cheers
Matt

You might wish to add that if using apache under unix a ‘chmod apache scriptname’ command will be needed to allow the script to run correctly.

Did you mean ‘chown apache scriptname’ ?

It depends on your server setup really. If you’re on a shared host then you probably don’t have the required permission to chown.

Usually ‘chmod 755 scriptname’ will do the job.

Cheers
Matt

I was wondering if anyone got this script working in OS X Server 10.4?

After I click submit the script brings me to the error page, rather than outputing the HTML in the end of script

hi, Is there a way to append the uploaded photo to an existing page of photos? rather a novice at cgi, if it’s possible ?

What’s the exact error message that you’re getting? (Take a look in your web server’s error log.)

Cheers,
Matt

Yes, you could extend the script so that each time an image is uploaded, the corresponding img tag is added to the page in question. Put marker comments in the page such as:


<!-- BEGIN IMAGES -->
.
.
.
<!-- END IMAGES -->

Your Perl script can then look for the <!-- END IMAGES –> marker and insert the new img tag just before it.

There’s a tutorial on our site that explains how to write to files in Perl.

Cheers,
Matt

I pretty much just copied and pasted your code into Notepad and saved the files as instructed, then uploaded them to the correct locations and changed permissions. Trying it out, I get an internal server error. I checked my log, and it says “Premature end of script headers” What does that mean? I’m completely ignorant when it comes to Perl/CGI scripting.

I got the same error “Premature end of script headers” it turns out that the perl script shall not have space before each line.

I pretty much just copied and pasted your code into Notepad and saved the files as instructed, then uploaded them to the correct locations and changed permissions. Trying it out, I get an internal server error. I checked my log, and it says “Premature end of script headers” What does that mean? I’m completely ignorant when it comes to Perl/CGI scripting.

Did you upload the file in ASCII (text) format?

Great article!

I do have two issues thus far though maybe someone can help me out with.
The first issue occurs with the script ran as is, only changing the directory information. The file uploads into the specified directory but throws an internal server error stating that the file was not found.

The second issue is running it -T where it throws a independency error. I get why it would do that but I am concerned about potential security risks involved not running the file in taint mode. I’m kind of wondering if I am being paranoid or if it is wise to untaint that data so it will run -T.

Any thoughts, information, links, etc. will be extremely helpful and much appreciated! :confused:

-Tina

Fixed part one by removing the leading spaces from the code. :wink:

I think it’s probably failing the taint checking on the filename. If you replace:

$filename =~ s/.*[\\/\\\\](.*)/$1/;

with something like the more thorough:


$filename =~ /(\\w+[\\.\\w]*)$/ or die ( "Filename was not in a recognized format." );
$filename = $1;

…then that should untaint the filename (hopefully!).

Cheers,
Matt