Uploading Files Using CGI and Perl

Assuming you uploaded the file to your cgi-bin folder, the you should probably be accessing it with a URL such as:

http://www.example.com/cgi-bin/upload.cgi

Not:

http://www.example.com/upload/upload.cgi

Does that help?

Matt

when i run the example script,it returns:-


Server error!

The server encountered an internal error and was unable to complete your request.

Error message:
Premature end of script headers: upload.cgi

If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.0.47 (Fedora)


do u have any solution?
_

You have an error in your script or in your server config. Look in your web server’s error log for the exact error message. Alternatively run the script from the command line (either on your server if possible, or on your local PC with Perl installed) to debug.

Matt

hi there. very helpful guide thanks. but how would i go about modifying it so that the uploaded files are emailed to me automatically, instead of being stored on my server?

thanks.
Ronny.

Ronny,

Check out the MIME-tools modules - they let you create emails, attach files (e.g. images) to them and send them.

Cheers,
Matt

Very useful guide. However, when I run it on my web server, upload some image file and click “submit form”, I can’t see the Thanks info. Weirdly, I can see the entire upload.cgi file in the explorer window. What’s going on?

lei,

Your web server hasn’t been set up properly to run CGI scripts. It’s just serving the upload.cgi file as if it’s a plain text file.

You need to tell your web server that it should treat files ending in .cgi as Perl scripts and execute them, rather than serve them as files.

Hope that helps :slight_smile:

Cheers,
Matt

it could be the path settings in the upload.cgi file.

you can change the <img src> line in the cgi script to this to remove the worry:

<img src=“$upload_dir/$filename” border=“0”>

It could also be the chmod setting.

Hi, I’m a novice at server-side scripting and I’ve a site on Unix / Linux server so I guess I need to make the PERL excecutable. But do I add the (chmod a+rx or chmod 755 ) to the file name, of does that go within the actual script somewhere.

I was also wondering how difficult it would be to create a Mp3 upload facility. Can you give me any advice?

Thanks,

Jim

Jim,

You can make your script executable using your FTP program. There will usually be an option to “set permissions on” or “chmod” the uploaded file.

As an MP3 is a file just like an image, you should be able to use the upload script in the tutorial to do what you need.

Matt

Hey, I’m having the same problem as someone who posted earlier. I got the script to work but when I upload files the contents show up empty. One difference is I’m using Windows (yeah I know I wish I wasn’t). And I’m hosting it on a machine myself so I have full access to apache. Any ideas? Thanks in advance.

Did you include the " binmode UPLOADFILE; " line as described in the article? If so then you possibly have some kind of permissions problem. Make sure that the web server user is allowed to create and modify files inside your upload folder.

Matt

I tried adding that line and it had no impact on this problem. I’m not too sure if the permissions are set correctly.

<Directory “C:/MATLAB701/toolbox/webserver/psws”>
AllowOverride None
Options Indexes ExecCGI
</Directory>

now the actual directory I have the file uploaded to is in the path
C:/MATLAB701/toolbox/webserver/psws/upload.

Do the permissions get applied to all subfolders?
Also I got rid of the “AllowOverride None” because I found out that defaults are given to folders without specific permissions.

Thanks…

I just noticed something interesting…Whenever I upload a file the temporary file gets written in the documentroot directory. If you change its extension to match the file you upload it turns out to be the complete file, so that means the script does properly upload the file, it just doesnt copy it to my upload folder within documentroot. Any ideas?

Ok, so I figured it out it was this line:

$query = new CGI->new;

I took out ->new and it worked.

I tried the file upload stuff in the Matt Doyle article “Uploading Files Using CGI and Perl” but there’s something I’m missing. Tried it out and got “The page cannot be found”. Here’s what I’ve observed: - file did not post to the server; - URL changed to domain/upload.cgi. I’m guessing that it’s not the path in the form action of the html file but rather the path in the $upload_dir in the cgi file. So, if the domain were quickbrownfox but the server root directory is Quick Brown Fox, shouldn’t the $upload_dir be “/Quick Brown Fox/upload”?

I have a problem in the upload location. my site is http://democrats.coretechnique.com/upload/ what would be the possible exact web adress? /home/coretechnique/democrats/upload ?

At what point to you get “The page cannot be found” ?

If you placed your upload.cgi in the cgi-bin folder then your URL in your FORM tag should be e.g. ‘/cgi-bin/upload.cgi’, not just ‘upload.cgi’.

$upload_dir should be the full path from your server’s volume root. For example, this might be '/home/username/Quick Brown Fox//htdocs/upload".

Hope that helps…

Matt

If you mean the full filesystem path to your ‘upload’ directory, then it would depend on how your server’s set up. Ask your web hosting company what the full path to your site’s document root should be (it’s probably also in their support area or web control panel if you have one). Then append ‘/upload’ to that path.

Cheers
Matt

I get 0 bytes files