html源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="zy.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile">
<input type="submit">
</form>
</body>
</html>
php源码:
<?php
header("content-type:text/html;charset=utf-8");
if(isset($_FILES["uploadfile"])){
$uploadfile=$_FILES["uploadfile"];
$filename=$uploadfile["tmp_name"];
$name=$uploadfile["name"];
$extpos=strpos($name,".")+1;
$ext=substr($name,$extpos);
if ($ext!="txt"){
echo "上传文件类型失败";
die();
}
$filesize=$uploadfile["size"];
if ($filesize>1024*2){
echo "文件过大,上传失败";
die();
}
$destination="upload/".$uploadfile["name"];
$destination=iconv("utf-8","gbk",$destination);
move_uploaded_file($filename,$destination);
效果测试:
上传2k以下txt文件:

上传2k以上txt文件:

上传其他文件:

本文详细介绍了一个使用HTML和PHP实现的文件上传功能,包括如何设置上传文件的类型和大小限制,以及上传后的文件处理过程。通过示例代码,展示了如何检查上传文件是否为txt格式,并确保其大小不超过2KB。
2159





