Uploading Files Using CGI and Perl

Sounds like your files aren’t getting uploaded properly. Maybe your script doesn’t have permission to write to the /tmp/ directory or other temp directory, so it can’t store the temporary uploaded file?

Your server error log might give you more information as to what’s going on.

Cheers,
Matt

ok. following your uploading script, everything works 100% (good job tho :stuck_out_tongue: ), all my uploads get an error. ) bytes and file headers missing or whatever.

is there anyway in your script that i can specify the temp directory?

All works fine, except that your “putting it all together” forgot the bin mode part. My problem lies in the temp files being created by the program. Any thoughts on deleting them?

Look for the section “You can read the name of the uploaded file, but can’t retrieve the data” in the CGI.pm documentation.

Cheers
Matt

CGI.pm should automatically remove temp upload files. However on Windows systems you might need to explicitly close the files as Windows can’t delete open files. You could try adding:


close $upload_filehandle;

towards the end of your upload script maybe?

Matt

I did exactly as described and I still get Internal server error.

This article was excellent

Thanks shashi_pagare :slight_smile:

Matt

When the binary file is uploaded you use
binmode UPLOADFILE;
and it works fine for the binary file in my web server.

What is the text file option ?
I am trying to use
use open IN => “:crlf”, OUT => “:raw”;
But my web server doesn’t like it.

Is there any option like
binmode UPLOADFILE; for text file?

Please give me a help.
Thanks.

Just leave out the binmode line.

Matt

great!!! at last I could get a nice explanation about an upload file.
Could you explain another example, but using size control over the upload file and other restrictions? thanks a lot!

Tried this. upload.cgi won’t load but if I rename file to upload.pl it runs. Only thing is no picture appears in upload directory and pic also does not appear on the web page. Any ideas? r.scott@iop.kcl.ac.uk

I have a similar problem. Nothing seems to be uploaded. I’m running FreebSD 5.3 (RELEASE) with perl 5.8.5 if that helps.

Could be a permissions problem maybe? Make sure the webserver user has permission to create files in your “upload” directory. Also check that the path to the “upload” directory as specified in your script is correct.

Matt

  1. use the following as the first line of the
    upload.cgi script:
    #!/usr/bin/perl
  2. I use the temp directory for my
    file-dir/file-name…
    $upload_dir = “/temp/”

Tom

Your $upload_dir should probably include the path to your web site from the volume root, eg:

$upload_dir = “/var/www/html/temp/”

Make sure the “temp” folder is writable by your webserver user too.

Matt

I see to be having a problem with the CGI file - everything appears to be acurate, but when clicking on the submit button - it closk then gives me a “the document contains no data” error - can you help? here is a link to the test upload page: http://www.god-song.com/eupload.html

plz can you help? when i click submit this comes up:
The specified CGI application misbehaved by not returning a complete set of HTTP headers.

what does this mean?

When I tried uploading a file at your site I got the error:

CGI open of tmpfile: Permission denied

Looks like your server’s CGI.pm isn’t set up with a temp directory that it can dump its temp files into. Look for the section “You can read the name of the uploaded file, but can’t retrieve the data” http://stein.cshl.org/WWW/CGI/ for some pointers.

Hope that helps :slight_smile:

Cheers,
Matt

It means that your script isn’t running correctly (usually because it has an error). Make sure you have specified the correct path to your upload folder, and also set the permissions for the folder correctly.

You can also find general troubleshooting info in our Troubleshooting CGI Scripts tutorial.

Cheers,
Matt