WebjxCom提示:还是继续前面的
PHP教程
,继续来学习
。 5.4文件上传
你可以利用
PHP实现文件的功能
,注意客户端的浏览器应该是Netscape3以上或者IE3以上
。以下就是该功能的简单演示:
(upload.html):
<HTML>
<HEAD>
<TITLE>UploadYourFile</TITLE>
</HEAD>
<BODY>
<FORMACTION="receiver.php3"
ENCTYPE="multipart/form-data"METHOD=POST>
<INPUTTYPE="HIDDEN"
NAME="MAX_FILE_SIZE"VALUE="2000000">
<INPUTTYPE="FILE"
NAME="uploadfile"SIZE="24"MAXLENGTH="80">
<BR><BR>
<INPUTTYPE="SUBMIT"VALUE="UploadFile!"
NAME="sendit">
<INPUTTYPE="SUBMIT"VALUE="Cancel"
NAME="cancelit"><BR>
</FORM>
<I><FONTSIZE="2">(Youmaynoticeaslight
delaywhileweuploadyourfile.)</FONT></I>
</BODY>
</HTML>
下面是处理上传的文件:
(receiver.php3):
<?
functiondo_upload()
{
global$uploadfile,$uploadfile_size;
global$local_file,$error_msg;
if($uploadfile=="none")
{
$error_msg="Youdidnotspecifyafileforuploading.";
return;
}
if($uploadfile_size>2000000)
{
$error_msg="Sorry,yourfileistoolarge.";
return;
}
$the_time=time();
//你需要对以下目录有写权限
$upload_dir="/local/uploads";
$local_file="$upload_dir/$the_time";
if(file_exists('$local_file'))
{
$seq=1;
while(file_exists("$upload_dir/$the_time$seq")){$seq++;}
$local_file="$upload_dir/$the_time$seq";
};
rename($uploadfile,$local_file);
display_page();
}
functiondisplay_page()
{
//这里是你的页面内容
}
<HTML>
<HEAD>
<TITLE>php3ReceivingScript</TITLE>
</HEAD>
<BODY>
<?
if($error_msg){echo"<B>$error_msg</B><BR><BR>";}
if($sendit)
{
do_upload();
}
elseif($cancelit)
{
header("Location:$some_other_script");
exit;
}
else
{
some_other_func();
}
?>
</BODY>
</HTML>