文件上传使用的是move_uploaded_file()函数,使用array_push()函数向数组中添加元素,使用array_unique()函数删除数组中的重复元素,使用array_pop()函数获取数组中最后一个元素,并将数组长度减1,使用count()函数获取数组的元素个数。
主要代码:
页面:
<table width="725" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="2"><img src="images/02-03.jpg" width="765" height="230"></td> </tr> <tr> <td width="662" align="center" valign="top"><table width="578" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="20" colspan="2" align="center" bgcolor="#FFFFFF"><img src="images/02-03 (1).jpg" width="561" height="42"></td> </tr> <tr> <td height="20" colspan="2" align="center" bgcolor="#FFFFFF" class="STYLE1">文件路径(5个文件以内任意上传)</td> </tr> <form action="index_ok.php" method="post" enctype="multipart/form-data" name="form1"> <tr> <td width="88" height="30" align="right" class="STYLE1">内容1:</td> <td width="369"><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td height="30" align="right" class="STYLE1">内容2:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td height="30" align="right" class="STYLE1">内容3:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td height="30" align="right" class="STYLE1">内容4:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td height="30" align="right" class="STYLE1">内容5:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td height="50"> </td> <td><input type="image" name="imageField" src="images/02-03 (3).jpg"></td> </tr> </form> </table></td> <td width="99"><img src="images/02-03 (2).jpg" width="204" height="362"></td> </tr> <tr> <td colspan="2"><img src="images/02-03 (4).jpg" width="765" height="80"></td> </tr> </table>
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++){ //根据元素个数执行for循环 $path="upfile/".$_FILES["picture"]["name"][$i]; //定义上传文件存储位置 if(move_uploaded_file($_FILES["picture"]["tmp_name"][$i],$path)){ //执行文件上传操作 $result=true; }else{ $result=false; } } if($result==true){ echo "文件上传成功,请稍等..."; echo "<meta http-equiv=\"refresh\" content=\"3; url=index.php\">"; }else{ echo "文件上传失败,请稍等..."; echo "<meta http-equiv=\"refresh\" content=\"3; url=index.php\">"; } ?>