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!
In reply to CGI file upload: Print to filehandle vs move file by tospo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |