php多文件上传,检查后缀和加密文件名

文件分为两部分html和php部分
html部分就不详细说明了

<html>
<head>
<meta http-equiv="content-type" content="text/html charset=uft-8"/>
<title></title>
<style>
.style1{
     
     
	font-size:12px;
}
input{
     
     
	font-size:12px;
	margin-left:5px;
	padding:5px;
}
input[type=submit]{
     
     
	width:80px;
	height:28px;
	margin:5px;
	font-size:14px;
	line-height:1em;
	background-color:#acacac;
	color:white;
	border:none;
}
</style>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="480px" bgcolor="#ffffff" bordercolor="#acacac">
<form action="uploadok.php" method="post" enctype="multipart/form-data" name="form1">
<tr>
	<td width="88px" height="30px" align="right" class="style1">内容1:</td>
	<td width="369px"><input name="picture[]" type="file" id="picture[]" size="30"></td>
</tr>
<tr>
	<td width="88px" height="30px" align="right" class="style1">内容2:</td>
	<td width="369px"><input name="picture[]" type="file" id="picture[]" size="30"></td>
</tr>
<tr>
	<td width="88px" height="30px" align="right" class="style1">内容3:</td>
	<td width="369px"><input name="picture[]" type="file" id="picture[]" size="30"></td>
</tr>
<tr>
	<td width="88px" height="30px" align="right" class="style1">内容4:</td>
	<td width="369px"><input name="picture[]" type="file" id="picture[]" size="30"></td>
</tr>
<tr>
	<td width="88px" height="30px" align="right" class="style1">内容5:</td>
	<td width="369px"><input name="picture[]" type="file" id="picture[]" size="30"></td>
</tr>
<tr>
	<td colspan="2" align="center"><input type="submit" name="submit" value="提交"/></td>
	
</tr>
</form>
</table>

</body>
</html>

下面是php部份uploadok.php

<?php
if(!is_dir("./upfile")){
    
      //判断是否有上传文件夹,没有就创建
	mkdir("./upfile");
}
array_push($_FILES['picture']['name'],'');  //创建文件数组,以在末尾添加一个空白形成数组
$array=array_unique($_FILES['picture']['name']);  //去掉重复
array_pop($array);   //再去掉末尾的空白数组值
for($i=0;$i<count($array);$i++){
    
    
	$first=explode(".",$_FILES['picture']['name'][$i]);  //.为后缀分割字符成数组
	$ext=strtolower(end($first));   //取得数组的最后一项
	$arr=array('jpg','png');  //建立以后缀格式的数组
	$b=in_array($ext,$arr);  //判断后缀是否在数组中存在,如果有返回true
	if($b){
    
    
		$name=md5($_FILES['picture']['name'][$i]).".".$ext;
		$path="./upfile\\";
		if(move_uploaded_file($_FILES['picture']['tmp_name'][$i],$path.$name)){
    
    
			$result=true;
			echo "文件上传成功,请稍等...";
			echo "<meta http-equiv=\"refresh\" content=\"3; url=1-13.php\">";
		}else{
    
    
			$result=false;
			echo "文件保存失败,请稍等...";
			echo "<meta http-equiv=\"refresh\" content=\"3; url=1-13.php\">";
		}
	}else{
    
    
		echo "文件格式不正确,请稍等...";
		echo "<meta http-equiv=\"refresh\" content=\"3; url=1-13.php\">";
	}

}
?>

结尾:有个bug,如果上传一个文件时只能用第一个,用第二个或其它的会报文件格式不正确,这就是bug出错,
多文件上传是正常

猜你喜欢

转载自blog.csdn.net/cdcdhj/article/details/113161932