Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comping files size

by bigup401 (Pilgrim)
on Jun 23, 2020 at 20:18 UTC ( [id://11118390]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: comping files size
by haj (Vicar) on Jun 23, 2020 at 21:18 UTC

    You need to read the section "Processing a file upload field" in the documentation of CGI, where you get detailed guidance. In particular, the param method gives you the name of the file at the client's side. You don't have a file on the server at that time, so you can't check its size with -s.

    Once you have fixed your form processing code, you might also want to check what you are actually printing to your OUTFILE handle.

Re: comping files size
by jcb (Parson) on Jun 24, 2020 at 02:31 UTC

    You finally are using strict and warnings. Those are at least two steps in the right direction.

    Other monks have already mentioned that you need to read the documentation for CGI or switch to one of the modern frameworks. CGI spools uploaded files to temporary files before continuing your script; this is a possible DoS vector if a client uploads very large files. The temporary files are created and the upload accepted before your code gets a chance to check their sizes. There is another interface that allows you to process the uploaded data as it is received, avoiding this problem; you will need to read the CGI documentation for details.

    You are not setting $allowed_size to a valid integer. An easy hint: integers do not require quotes in Perl. A much better way to represent 5MiB is 5 * (1<<20) or 5 * 1024 * 1024, depending on the familiarity of other programmers on the project with powers of two.

    Your file-handling code makes no sense; read the documentation for rename and CGI. You are also printing (very confusingly) the name of the image to the output file, which is useless here.

    While your use of for and last is clever, you really should use a single $status variable and a series of if or unless blocks. Something like:

    my $status = ''; # note that '' is a false value # ... unless (image_has_good_type) { $status = "bad image type"; } unless ($status or image_not_empty) { $status = "empty image"; } unless ($status or image_too_long) { $status = "image too long"; } unless ($status) { # accept upload $status = "success"; } # ... print <<END_HTML; # ... <pre>$status</pre> # ... END_HTML

    Using literal HTML in your script like that is also a recipe for confusing problems; you would do far better to put the HTML in a __DATA__ section and process it using Template::Toolkit or similar. The convention for here documents is for the end marker to read as an end marker; using START_HTML here is confusing.

Re: comping files size
by Corion (Patriarch) on Jun 23, 2020 at 20:59 UTC

    Please tell us how your program fails for you.

    Also, you should know by now to reduce your program and remove unnecessary parts.

    For example, does your program still have the same problem if you remove the CGI upload part?

    Does the program output the correct file size for a given file?

    Do you know how Perl compares numbers? What are allowed characters in a number?

Re: comping files size
by perlfan (Vicar) on Jun 24, 2020 at 00:47 UTC
    To make it easier on yourself, have a page to display just the HTML form. This could be just an upload-page.html, no CGI needed there. You need to set the action (method=post) to the CGI page, action=/process-upload.cgi let's call it. As long as you have your environment set up properly (looks to be local Windows?), then you'll be on your way to debugging.

    I recommend some things:

    • only mix the form and the processor when you have the form/processor model firm in your head
    • if on Windows, use a docker image with apache or nginx set up to do cgi
    • better yet, dump .cgi and use Dancer2 or Mojo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11118390]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found