php多檔案上傳,檢查字尾和加密檔名
阿新 • • 發佈:2021-01-27
檔案分為兩部分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出錯,
多檔案上傳是正常