In addition to the links above from t0mas, the way I have used in the past is via cgi->tmpFileName and using File::Copy,

A subroutine example is below, msg me if you cant work out which bits might be useful.

P.S I have a suspicion setting the umask to 000 might not be a good thing but this sub is renaming the files...

sub storeFiles { my $self = shift; my $cgi = shift; my $mode = shift; # going to be insert or update my $fileerror = '0'; my @filecols = ('DOC_1', 'DOC_2', 'IMG_1', 'IMG_2', 'IMG_3', 'IMG_ +4'); my $umm = umask(000); foreach my $doc (@filecols) { if ($cgi->param($doc)) { # if insert this is an add, else a replace action for upda +tes my $longfile = $cgi->param($doc); my $tempfile = $cgi->tmpFileName($longfile); my $size = -s $tempfile; if (($size < 1) || ($size > 2000000)) { $fileerror = '1'; last; } $longfile =~ s/ //g; # strip sp +aces $longfile =~ s/^(.*[\/\\:])*([^\/\\:]+)$/$2/; # remove p +ath if ($longfile !~ /\./) { $fileerror = '2'; last; } $longfile =~ s/\.(?=.*\.)//g; # substitu +te all but last dot (courtesy of L.Rosler) my ($f, $ext) = split /\./, $longfile; if (!$self->_isValidFile($doc, $ext)) { $fileerror = '3'; last; } my $newfile = $DATA_DIR . $self->{'PAGE'}[2] . "/" . $self +->{'DOCID'}[2] . $doc . "." . $ext; eval { copy ($tempfile, $newfile) or die "cant copy temp *$te +mpfile* to new *$newfile* $! \n longfile is $longfile"; }; if ($@) { $fileerror = '4'; last; } else { $self->{$doc}[2] = $self->{'DOCID'}[2] . $doc . "." . +$ext; } } elsif ($cgi->param("exists$doc")) { $self->{$doc}[2] = $cgi->param("exists$doc"); } else { $self->{$doc}[2] = ''; } if (($cgi->param("chk$doc")) && (!$cgi->param($doc))) { # unlink file if update checkbox is checked my $page = $self->{'PAGE'}[2]; if (($page == 22) || ($page == 26) || ($page == 27)) { my $dir = "../data/$page"; opendir (DIRH, $dir) or Util->showError("Can't Open de +signated directory $dir $!"); my @files = grep /^$self->{'DOCID'}[2]$doc/, readdir D +IRH; @files = map ("$dir/$_", @files); closedir DIRH; unlink @files or Util->showError("oops cant unlink you +r chosen files @files $!"); } $self->{$doc}[2] = ''; } } umask($umm); return $fileerror; }

In reply to RE: Uploading via HTML/Using CGI.pm by agoth
in thread Uploading via HTML/Using CGI.pm by radagast

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.