read PROCESSING-A-FILE-UPLOAD and section Accessing the temp files directly.

If you use open to create the newfile there is no renaming required.

#!/usr/bin/perl use strict; use warnings; use CGI; use File::Copy; use CGI::Carp 'fatalsToBrowser'; my $q = CGI->new; my $photo = $q->param("pic"); my $result; if ($photo){ my ($ext) = $photo =~ /([^.]+)$/; my $copydir = '/full/path/to/web/img/'; my $newfile = $copydir."011190.$ext"; my $upload_fh = $q->upload("pic"); open OUTFILE,'>',$newfile or die "$!"; binmode OUTFILE; while (<$upload_fh>){ # $upload_fh as filehandle print OUTFILE; } close OUTFILE; $result = "$photo written to $newfile"; # Alternative using copy #my $tmpfile = $q->tmpFileName($upload_fh); #if ( copy($tmpfile,$newfile) ){ # $result = "File $photo uploaded to $newfile. # Temp file : $tmpfile"; #} else { # $result = "ERROR copying $tmpfile to $newfile"; #} } print $q->header(),$q->start_html("Upload Test"); print << "HTM"; <h2>File upload</h2> <div style="background-color:yellow">$result</div><br/> <form method="post" action="" enctype="multipart/form-data"> Filename: <input type="file" name="pic"/><br/><br/> <input type="submit"/> </form> HTM print $q->end_html;
poj

In reply to Re^5: file handing by poj
in thread file handing by bigup401

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.