tospo has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
When I want to upload a file in a cgi script, I have always used the technique described in all the tutorials I have found so far, i.e. get the filehandle of the the uploaded temp file, traverse it with a while loop and print to a new opened filehandle like this:
my $upload_filehandle = $query->upload("TheFile"); open ( UPLOADFILE, ">$target_filename" ) or die "$!"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;
Now I have recently seen some code where the name of the tempfile that CGI.pm creates is accessed directly, which is also documented in the CGI.pm docs:
$filename = $query->param('uploaded_file'); $tmpfilename = $query->tmpFileName($filename);
So the code I've seen simply moves this file to a new location with a single command (rename or File::Copy) instead of the above loop over its contents and print to new openend filehandle.
To me, moving the file actually seems a) simpler (less code) and b) better for performance than the loop construct but since all tutorials I have found teach the first version I am a bit uneasy about just using it in case there is a good reason that I am missing.
Could somebody please shed some light on this for me? Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI file upload: Print to filehandle vs move file
by rowdog (Curate) on Oct 16, 2009 at 22:06 UTC | |
by tospo (Hermit) on Oct 19, 2009 at 11:34 UTC | |
by rowdog (Curate) on Oct 20, 2009 at 02:52 UTC | |
|
Re: CGI file upload: Print to filehandle vs move file
by stonecolddevin (Parson) on Oct 19, 2009 at 19:13 UTC | |
by tospo (Hermit) on Oct 20, 2009 at 08:12 UTC | |
|
Re: CGI file upload: Print to filehandle vs move file
by Anonymous Monk on Oct 16, 2009 at 16:58 UTC | |
by tospo (Hermit) on Oct 19, 2009 at 11:27 UTC |