perleager has asked for the wisdom of the Perl Monks concerning the following question:

Hey

I'm having some trouble setting the maximum file size allowed to upload.

Here is my upload portion of the script:

use CGI; $CGI::POST_MAX = 1536000; #1.5MB my $upload_dir = "C:/Web Sites/Projects/Uploading/datapath"; my $business_plan_file = $results->valid('file_name'); #I'm using Data::FormValidator, $results->valid('file_name') is th +e input from the upload field in my form. $business_plan_file =~ s/.*[\/\\](.*)/$1/; #retrieves filename +w/out users full path my $upload_filehandle = $results->valid('file_name'); open UPLOADFILE, ">$upload_dir/$business_plan_file" or die; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;


When I attatch a large file, it still uploads the file even though its over 1.5 MB. It seems everytime I try a new module I run into these kind of newbie problems =( Can anyone tell me whats wrong with my code? Is there any way I can specify the error message if it dies if the file being uploaded is to surpasses 1.5mb?

Also, is there another way to set it so the uploads only allow a specific type of file based on its MIME type? This script will be used for only to upload Word Documents and Excel files. One way I thought about doing this is to split the filename and retrieve the extension, but then again that is not the safest way.

Thank you,
Anthony

Replies are listed 'Best First'.
Re: Setting max upload size/MIME types
by Tomte (Priest) on Mar 17, 2004 at 08:19 UTC

    More as a side-note: Do you have encoding="multipart/form-data" set as attribute of the form-tag? I ask 'cause you shouldn't retrieve the file-name with the complete user-side path. If you haven't set it, the browser does not transfer the file-contents, which would explain why you don't hit the set limit.

    Also note, that POST_MAX doesn't set the maximum file-size, but the maximum size of the complete POST-data. That could cause trouble if you want to allow the upload of more than one file within a single form....

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: Setting max upload size/MIME types
by iburrell (Chaplain) on Mar 17, 2004 at 19:53 UTC
    Did you check if there was an error reported by CGI? Did you receive the large file? A POST larger than the limit should report an error through the error method.
    my $error = $cgi->error(); if ($error) { die $error; }
    To get the MIME type that the browser report, use the uploadInfo mehod.
    my $file = $cgi->param('upload'); my $content_type = $cgi->uploadInfo($file)->{'Content-Type'};
      Hey,

      I had previously added the form attribute required to upload.

      Yes, the larger file ends up being uploaded.

      I will try you playing around with my coding with your following suggestions and will update this thread if succesfull for others to use for future referrence.

      Thanks,
      Anthony