Tony Cheung wrote: > Thanks for your help. > I got a program yesterday,and add in it,but program is not word! > #!e:\perl\bin\perl > #如果用CGI方式来行动这个文件,请把下段路径改成你的机器配置 > use CGI; > $upfilecount = 1; > $maxuploadcount = 2; #限制上传文件的最大数 > $basedir = "e:/English"; #上传的文件存放地址 > $allowall = "no"; #是否不限制文件后缀上传 > @theext =(".zip",".exe",".gif",".rm",".doc",".xls"); #要限制的文件后缀名 > $filesize = 220842; #限制文件大小在216K > print "Content-type: text/html\n\n"; > while ($upfilecount <= $maxuploadcount) { > my $req = new CGI; > my $file = $req->param("FILE$upfilecount"); > if ($file ne "") { > my $fileName = $file;
You can't check the file size until it's been uploaded. I think CGI would have stored the file in a temporary file at this point in time and you can use : my $tmpfile = $cgi->tmpFileName($file); my $file_size = -s $tmpfile; CGI can be configured to reject files greater than a given size : $CGI::POST_MAX = $filesize; # prior to the while loop > my $file_size = -s $file; > print $file_size."\n"; > if ($file_size < $filesize) { > > $fileName =~ s/^.*(\\|\/)// <file://\\|\/)//>; > #用正则表达式去除无用的路径名,得到文件名 > my $newmain = $fileName; > my $filenotgood; > if ($allowall ne "yes") { > $extname = > lc(substr($newmain,length($newmain) - 4,4)); #取后缀名 > for(my $i = 0; $i < @theext; $i++){ > #这段进行后缀名检测 > if ($extname eq $theext[$i]){ > $filenotgood = "yes"; > last; > } > } > } > if ($filenotgood ne "yes") { #这段开始上传 You may be able to just move the temp file rather than copying it. > open (OUTFILE, ">$basedir/$fileName"); > binmode(OUTFILE); > #务必全用二进制方式,这样就可以放心上传二进制文件了。而且文本文件也不会受干扰 > while (my $bytesread = read($file, my > $buffer, 1024)) { > print OUTFILE! $buffer; > } > close (OUTFILE); > $message.=$file . " 已成功上传!<br>\n"; > } > else{ > $message.=$file&nb! sp; . " > 文件后缀不符合要求,上传失败!<br>\n"; > } > > }else{ > $message.=$file . " > 文件后缀不符合要求,上传失败!<br>\n"; > } > } > $upfilecount++; > > } > print $message; #最后输出上传信息 > > > */Beckett Richard-qswi266 <[EMAIL PROTECTED]>/* wrote: > > All you need to get the file size is -s. > > I.e. $file_size = -s $file > > Save the following script as size.pl, and run like this: > > perl size.pl size.pl > > use strict; > use warnings; > my $file = $ARGV[0]; > my $file_size = -s $file; > print "$file is $file_size bytes\n"; > > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf > Of Tony Cheung > Sent: 03 November 2004 06:54 > To: perl-win32-users > Subject: How to obtain filesize? > > > I have a perl program about update file from pc to mysql,but I want > to limit filesize,how to obtain filesize and file type? -- ,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED] (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff) _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs