muyiyang2 发表于 2003-11-4 12:59:28

perl的上传文件

小弟写了一个用perl上传文件的小程序:
use CGi;
use fcntl qw (:DEFAULT :flock );
use constant UPLOAD_DIR =>'C:\ProgramFiles\ApacheGroup\Apache\htdocs\img';
use constant BUFFER_SIZE =>1024;

my $q=new CGI;
$q->cgi_error and error($q,"Error transferring file:". $q->cgi_error);
my $file =$q->param("file") || error($q,"No file received");
my $filename =$q->param("filename") || error($q,"No filename entered");
my $fh =$q->upload($file);
my $buffer ="";
$filename=~s/[^\w.-]/_/g;

if($filename=~/^(\w[\w.-]*)/)
{
$filename=$1;

}
else
{
error($q,"Invalid file name;files must start with a letter or number");
}

$outputfile=UPLOAD_DIR."\\".$filename;
#print $outputfile;
if (!open(OUTPUT,">$outputfile"))
{
error($q,$outputfile);

}

while(read($fh,$buffer,BUFFER_SIZE))
{
binmode $fh;
binmode OUTPUT;
print OUTPUT $buffer;
}

close OUTPUT;
print $q->header("text/html");
print $q->start_html("success");
print $q->h1("upload success");
print $q->end_html;
exit;
sub error
{
my ($q,$reason)=@_;

print $q->header("text/html");
print $q->start_html("Error");
print $q->h1("Error");
print $q->p("Your upload was not procesed because the following error occured");
print $q->p($q->i($reason));
print $q->end_html;
exit;

}

发现用这个程序上传的文件字节数位0,不知为啥,请各位大侠指教

jiangtao9999 发表于 2003-11-10 16:16:41

HTML部分呢?
PHP里上传文件要给有文件框的 form 加参数 ENCTYPE="multipart/form-data"
页: [1]
查看完整版本: perl的上传文件