Blogger Information
Blog 250
fans 3
comment 0
visits 322843
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传类型及文件上传实操
梁凯达的博客
Original
1234 people have browsed it

实例

<?php
header("Content-Type: text/html;charset=utf-8");

// 1:文件上传的类型必须为post 
// 2:允许的数据类型必须是 multipart/form-data
// 如果想要在前面的页面处理上传的话,action可能会有三种方法:
// 1、为空默认当前脚本
// 2、书写当前的脚本名
// 3、用正确的方法进行编写
?>
<html>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post"  enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="542488">
	<fieldset align="center">
		<legend align="center">文件上传</legend>
		<p><strong>选择文件:</strong><input type="file" name="upload"></p>
	</fieldset>
	<p align="center"><button type="submit">提交</button></p>
</form>
</html>

<?php
	//$_FILES:他是一个二维数组
	//$_FILES['当前上传的文件夹名称',input[name="upload"]]
	//$_FILES['upload']['name']:文件原始名称
	//$_FILES['upload']['type']:文件类型
	//$_FILES['upload']['size']:文件大小
	//$_FILES['upload']['tmp_name']服务器上的临时文件夹
	//$_FILES['upload']['error']:错误代码


	//检测是否POST过来的
	if($_SERVER['REQUEST_METHOD']=='POST'){
			//判断文件是否上传过来
		if(isset($_FILES['upload'])){
			//设置文件类型为四种图片上传类型
			$allow = ['image/jpg','image/jpeg','image/png','image/gif'];
			//判断是否为这种文件类型,是的话才让他进来
			if(in_array($_FILES['upload']['type'],$allow)){
				//移动文件到指定的目录当中
				move_uploaded_file($_FILES['upload']['tmp_name'],"./upload/{$_FILES['upload']['name']}");
			}else{
				//如果文件类型错误则进入假区间
				echo "<script>alert('您输入的文件类型为非图片类型')</script>";
				exit;
			}
		}
		var_dump($_FILES['upload']['error']);
		exit;
		//对文件上传失败的进行验证
		if(isset($_FILES['upload']['error'])){
			switch($_FILES['upload']['error']){
				//case为0时则文件上传成功
				case 0:
					echo '文件上传成功';
					break;
				case 1:
					echo '文件超出了php.ini中的大小';
					break;
				case 2:
					echo '文件超过表单中设置的大小';
					break;
				case 3:
					echo '文件只有一部分上传了';
					break;
				case 4:
					echo '没有文件被上传';
					break;
				case 6:
					echo '缺乏临时文件夹';
					break;
				case 8:
					echo '上传意外终止';
					break;
				default:
					echo '未知错误';
					break;
			}
		}
	}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post