html端:
<form action="check.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1024000">
<input type="file" name="file">
<input type="submit" value="upload">
</form>
其中,隐藏的input用于限制上传文件大小 不得超过1MB
php端:
$file = $_FILES['file'];
$tmpname = $file['tmp_name']; //文件临时存储路径名 /home/php0e23.txt
$filename = $file['name']; //文件名 a.txt
$filetype = $file['type']; //文件类型 text/plain
if($file['size']>2048000){ //文件大小超过2MB
echo '文件过大';
exit();
}
if($file['error']){ //存在错误
echo '未获取到上传的文件';
exit();
}
if(is_uploaded_file($tmpname)){ //临时文件存在
$mvd = move_uploaded_file($tmpname,$filename); //移动到自定义的位置
if(!$mvd){
echo '上传失败,文件转存过程出错';
exit(500);
}
}