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文件:
上传其他文件: