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

Hi all, I'm very new to CGI.pm - but love it.

I have been researching around this topic and the first part of it (the upload and all the options around...) it's pretty clear.
What I found complex is to write the uploaded file, like $file = param('upload'), in a specific directory, which path is determined by other input fields in the same form.

Assume I'll have to:
1. pass each param as a variable and then
2. built the path using $param1/$param2/... and
3. write the uploaded file into the built path.

Any pointers on the best way/command to write?

Thanks

  • Comment on How to write an uploaded file in a specific dir

Replies are listed 'Best First'.
Re: How to write an uploaded file in a specific dir
by Coruscate (Sexton) on Jan 20, 2003 at 09:41 UTC

    Disclaimer: very unsafe script for any kind of use. Will overwrite any file that you give a path for. Be careful if you test it. Don't be uploading to a path of '/etc/passwd' if you're root :)

    #!/usr/bin/perl -w use strict; use CGI ':standard'; # Print out the html page sub print_html { my ($title, $msg) = @_; print header, start_html($title), start_multipart_form, p($msg), table( Tr( th( 'Upload Path:' ), td( textfield({ name => 'upload_path', default => './upload.txt' }) ) ), Tr( th( 'File to Upload:' ), td( filefield({ name => 'upload_field' }) ) ), Tr( { colspan => 2 }, th( submit({ value => 'Upload File' }) + ) ) ), end_form, end_html; exit; } print_html( 'Upload File', 'Please select the file you wish to upload:' ) unless defined param('upload_field'); my $fh = upload('upload_field'); open UPLOAD, '>' . param('upload_path'); binmode UPLOAD; print UPLOAD while <$fh>; close UPLOAD; print_html( 'Upload Complete', "File $fh has been uploaded.<br /> You can now upload another file if you wish:" );

Re: How to write an uploaded file in a specific dir
by Tommy (Chaplain) on Jan 21, 2003 at 08:21 UTC