how to "attach file" in cgi-bin form??

  • pony
  • Newbie
  • Newbie
  • pony
  • Posts: 7

Post 3+ Months Ago

how can I add the option to "attach a file" in a form working in cgi-bin?
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Ok, for building a form which is able to send files

Code: [ Select ]
<form enctype="multipart/form-data" action="_URL_" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
  1. <form enctype="multipart/form-data" action="_URL_" method="post">
  2. <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
  3. Send this file: <input name="userfile" type="file" />
  4. <input type="submit" value="Send File" />
  5. </form>


I don't know how to handle such uploads in CGI, but you can usi this form and look for a variable named "userfile" containig the temporary location of the uploaded file. I think it can be an array with extra information.
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

what cgi language are you dealing with? If it's perl, here's a link from my bookmarks:
http://perlfect.com/articles/upload.shtml

However, a word of warning with that script:
Allowing the user to select a directory like in that script is VERY insecure. I would suggest either removing the select directory thing entirely or checking to see if the directory is either "images" or "sounds" before actually writing the file. If you were removing the directory selection completely, you would change the perl code to this:
Code: [ Select ]
#!/usr/bin/perl
use CGI;
my $cgi = new CGI;
my $file = $cgi->param('file');
$file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
my $name = $2;
open(LOCAL, ">$name") or die $!;
while(<$file>) {
  print LOCAL $_;
}
print $cgi->header();
print "$file has been successfully uploaded... thank you.\n";
  1. #!/usr/bin/perl
  2. use CGI;
  3. my $cgi = new CGI;
  4. my $file = $cgi->param('file');
  5. $file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
  6. my $name = $2;
  7. open(LOCAL, ">$name") or die $!;
  8. while(<$file>) {
  9.   print LOCAL $_;
  10. }
  11. print $cgi->header();
  12. print "$file has been successfully uploaded... thank you.\n";

Note that i Just modified that just then, off the top of my head. I'm not sure whether the directly above code works or not

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 56 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.