ファイルアップロード

実際はアップロードではないですが、上手く表現できないので。

[intro]
で指定されたファイルを読み込む方法


[HTML]

  1. formタグ中にenctype="multipart/form-data"オプションを追加する
  2. name値に"file"は使えない

[perl サンプルコード]

  1. textファイルだけ扱う
  2. 600Kb(2048*300)を読み込む上限とする
#!/usr/local/bin/perl
use strict;
use CGI;
use constant BUFF_SIZE  => 2048;
use constant READ_LIMIT => 300;

eval{
 my $cgi = new CGI;
 print $cgi->header( -type    => 'text/html',
                     -charset => 'utf-8' );

 # filename
 my $filename = $cgi->param('File');
 print "<p>filename = $filename</p>\n\n";

 # mime-type
 my $type;
 $type = $cgi->uploadInfo($filename)->{'Content-Type'} if($filename);
 unless ($type =~ /^text\//) {
   print "<p>This file is not text type!! - $type</p>\n\n";
   return;
 }

 # read file
 my ($file, $bytesread, $buffer,$file_size);
 my $file_size = 0;
 while($bytesread = read($filename, $buffer, BUFF_SIZE)){
   $file .= $buffer;
   $file_size++;
   if( READ_LIMIT < $file_size ){
     print "<p>file-size over!! Please make it to 600Kb or less.</p>\n\n";
            $file = undef;
     last;
   }
 }
 print "---start---<BR>\n\n";
 print "$file<BR>\n\n";
 print "---ended---<BR>\n\n";

 print "<p>END</p>";
 1;
};

if( $@ ){
 print "Exception: $@";
}

1;
__END__


表示が崩れていますが、ご愛嬌で。。
書式が間違ってるんだと思うけど、使ってるテンプレートとタグの相性とか・・・


[参考URI]
http://www.harukaze.net/~mishima/perl/module/upload_by_CGI_pm.html
http://www.topstudio.co.jp/~kmuto/html/node38.html
http://www.ss.iij4u.or.jp/~somali/web/_perl_upload.html