<span class="unnamed3">文件提交页面既已生成,下面任务就很明确了:将提交的文件内容保存到服务器上。 <br><br>下面我们用两种方法来实现这个功能: <br><br>1. 用 PHP 来保存: <br>我们先定义一个文件保存函数 fup() 它有两个参数: <br> $filename: 文件内容 <br> $fname: 文件名(包含路径) <br>剩下的就是写一个循环将文件依次写入服务器。这里要简单说明一下: <br>PHP 对于上传文件的处理是这样的:如果提交的文件框我为 file0, 那么提交给 PHP 的文件内容保存在变量 $file0 中,而文件名则保存在 $file0_name 中。这样在这个循环中我要做的就是将提交页面提交的内容分解出来,实现过程请看下面的代码。 <br><br>fileup.php <br>---------------------------------------------------------------------- <br> <br> function fup($filename,$fname) <br> { If($filename != "none") { <br> copy($filename,$fname); <br> unlink($filename); <br> } <br> } <br><br> for($i=0;$i { <br> $ffnn="file".$i; <br> $ffnnname=$ffnn."_name"; <br> $ffpath="path".$i; <br> <br> //print $$ffnn; <br> print $$ffnnname; <br> print "<br>"; <br> <br> fup($$ffnn,$$ffpath.$$ffnnname); //"../www/test/tmp/" <br> } <br>?> <br>---------------------------------------------------------------------- <br><br>2. 用 PERL 来保存: <br>它们实现的原理完全一样,在此不多说,请看代码: <br><br>fileup.cgi(fileup.pl) <br>---------------------------------------------------------------------- <br>#!/usr/bin/perl <br><br>use CGI qw/:standard/; <br><br>if ($ENV{'CONTENT_TYPE'} !~ /multipart/form-data/) { <br> print "Cache-Control: no-cachenPragma: no-cachen" . <br> "Content-Type: text/htmlnn" . <br> "Your web browser cannot upload files. Sorry."; <br> exit 0; <br>} <br><br><br>$cntfile=param('cnt'); <br>print header; <br>print start_html; <br>#print "Receiving Please wait...."; <br><br>&g_head; <br><br>#$writed = '../www/test/tmp/'; <br><br>for ($i=0;$i $paramfile = 'file'.$i; <br> $parampath='path'.$i; <br> <br> $writed=param($parampath); <br><br> &upfile; <br> &g_body; <br>} <br><br>&g_bott; <br><br>#<br>sub upfile <br>{ <br> $maxdata = 512000; <br># $writed = '../www/test/tmp/'; <br><br><br> $strRFname=reverse $xfile; <br> $intIndex=index($strRFname,'\'); <br> $strNetFname=substr($strRFname,0,$intIndex); <br> $strNetFname=scalar reverse $strNetFname; <br> <br> <br> if((stat $xfile)[7]>$maxdata){ <br> print "Status: 411 Size Not Allowedn" . <br> "Content-Type: text/htmlnAllow: POSTnn" . <br> "<title>411 411 Size Not Allowed</title> <h1> You got big problem. Try again.</h1>n"; <br> exit 0; <br> } <br> <br> <br> binmode $xfile; <br> use File::Copy; <br> copy($xfile,$writed.$strNetFname); <br>} <br><br>sub g_head{ <br> print '<table border="1" align="center">'; <br> print '<tr><td colspan="3" align="center">文件上传结果(Upload Result)</td></tr>'; <br> print '<tr align="center">'; <br> print ' <td>SourceFile:</td>'; <br> print ' <td>DestFile:</td>'; <br> print ' <td>Upload</td>'; <br> print '</tr>'; <br>} <br><br>sub g_body{ <br> print '<tr>'; <br> print ' <td>'.$xfile .'</td>'; <br> print ' <td>'.$writed.$strNetFname.'</td>'; <br> print ' <td>OK!</td>'; <br> print '</tr>'; <br>} <br><br>sub g_bott{ <br> print '</table>'; <br>} <br>---------------------------------------------------------------------- </span> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br> </p> <p style="width:100%;text-align:center;margin:10px 0"> </p> <p class="clear"></p>