Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Question regarding basic help with my upload script

by tf198 (Initiate)
on Apr 02, 2005 at 21:40 UTC ( #444439=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Monks,
I am a newbie seeking your advice. I am trying to add some settings to my upload script posted below. At this time it works really great, but fails to allow me to control any conditions such as:
1. set allowed ext. ie (.gif,.jpg,.jpeg)
2. control upload size. ie (max=2000)
+ anything you may feel I should also consider

I have tried many times the very best I can but still can't seem to get this right. Please help me add the conditions mentioned above for my own personal knowledge and security.

Thank you very much for your time~
Jim

#!/usr/bin/perl use CGI qw(:standard); my $cgi = new CGI; my $upload_dir = '.'; my $file = $cgi->param('file'); my $filename = $file; $filename =~ s/^.*(\\|\/)//g; print $cgi->header(-type=>'text/html'); open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!"; + while(<$file>) { print OUT; } close(OUT); print "$filename has uploaded\n"; __END__

20050402 Janitored by Corion: Fixed code tags

Replies are listed 'Best First'.
Re: Question regarding basic help with my upload script
by tlm (Prior) on Apr 02, 2005 at 21:54 UTC

    Look at the sample script posted here.

    Also, your use of <code></code> tags, is not quite right. It should be like this:

    <code>
    #
    # Your code here
    #
    </code>

    Review the PM site FAQ, especially Writeup Formatting Tips.

    the lowliest monk

Re: Question regarding basic help with my upload script
by nobull (Friar) on Apr 03, 2005 at 12:24 UTC
    Question regarding basic help with my upload script

    Good subjects are important. Please try to leave out words that do not significantly contribute to the meaning and use the space saved to add ones that do. For example, in your subject the following does not significantly contribute to the meaning: "Question regarding basic help with my ... script".

    ...fails to allow me to control any conditions such as:
    1. set allowed ext. ie (.gif,.jpg,.jpeg)
    2. control upload size. ie (max=2000)

    If you want to limit the upload size to avoid a DoS attack then set $CGI::POST_MAX.

    If you are asking how to tell the browser that you want it to restrict what it invites the user to upload then you need to look at the HTML of the upload form, not the CGI script that processes the HTTP submission afterwards. (This has nothing to do with Perl).

    If you are asking how to check if a string ends with a particular sequence of characters then perhaps something like:

    $filename =~ /\.(gif|jpg|jpeg)$/

    If you are asking how to tell how big a file associated with a filehandle is then you could use something like:

    -s $file
    anything you may feel I should also consider
    open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!"; + while(<$file>) { print OUT; } close(OUT);
    You should consider that the fact that GIF and JPEG are binary formats.

    As such you should put the OUT filehandle into binary mode. Also you should not try to copy the file a line of text at the time - because it is not made up of lines of text.

    { local *OUT; # Or better yet use lexical filehandle! open(OUT, ">$upload_dir/$filename") or die "Fail to open $filename: + $!"; binmode(OUT); local $/=\1024; # A block at a time local *_; # Don't stomp on $_ while(<$file>) { print OUT; } close(OUT) or die "Error writing $filename: $!"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-12-02 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (18 votes). Check out past polls.

    Notices?