整体文件视图:
代码文件名:test.php
<!doctype html> <html> <head> <meta charset="utf-8"> <title>多文件上传</title> <style type="text/css"> body { width: 600px; margin: 40px auto; font-family: 'trebuchet MS', 'Lucida sans', Arial; font-size: 14px; color: #444; } table { *border-collapse: collapse; /* IE7 and lower */ border-spacing: 0; width: 100%; } table { border: solid #ccc 1px; -moz-border-radius: 6px; -webkit-border-radius: 6px; border-radius: 6px; -webkit-box-shadow: 0 1px 1px #ccc; -moz-box-shadow: 0 1px 1px #ccc; box-shadow: 0 1px 1px #ccc; } table tr:hover { background: #fbf8e9; -o-transition: all 0.1s ease-in-out; -webkit-transition: all 0.1s ease-in-out; -moz-transition: all 0.1s ease-in-out; -ms-transition: all 0.1s ease-in-out; transition: all 0.1s ease-in-out; } table td, table th { border-left: 1px solid #ccc; border-top: 1px solid #ccc; padding: 10px; text-align: left; } table th { background-color: #dce9f9; background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9)); background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9); background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9); background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9); background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9); background-image: linear-gradient(top, #ebf3fc, #dce9f9); -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; -moz-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; border-top: none; text-shadow: 0 1px 0 rgba(255,255,255,.5); } table td:first-child, table th:first-child { border-left: none; } table th:first-child { -moz-border-radius: 6px 0 0 0; -webkit-border-radius: 6px 0 0 0; border-radius: 6px 0 0 0; } table th:last-child { -moz-border-radius: 0 6px 0 0; -webkit-border-radius: 0 6px 0 0; border-radius: 0 6px 0 0; } table th:only-child { -moz-border-radius: 6px 6px 0 0; -webkit-border-radius: 6px 6px 0 0; border-radius: 6px 6px 0 0; } table tr:last-child td:first-child { -moz-border-radius: 0 0 0 6px; -webkit-border-radius: 0 0 0 6px; border-radius: 0 0 0 6px; } table tr:last-child td:last-child { -moz-border-radius: 0 0 6px 0; -webkit-border-radius: 0 0 6px 0; border-radius: 0 0 6px 0; } </style> </head> <body> <h1>多文件同传(5个文件以内任意上传)</h1> <table> <form action="ok.php" method="post" enctype="multipart/form-data" name="form1"><!--默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据--> <tr> <td>内容1:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td><!--name以数组picture[0]、picture[1]的形式向服务器ok.php传递文件--> </tr> <tr> <td>内容2:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td>内容3:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td>内容4:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td>内容5:</td> <td><input name="picture[]" type="file" id="picture[]" size="30"></td> </tr> <tr> <td colspan="2"><input type="image" name="imageField" src="images/btn.jpg"></td> </tr> </form> </table> </body> </html>代码文件名:ok.php
<?php if(!is_dir("./upfile")){ //判断服务器中是否存在指定文件夹 mkdir("./upfile"); //如果不存在,则创建文件夹 } array_push($_FILES["picture"]["name"],""); //向表单提交的数组中增加一个空元素,$_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=\"1; url=test.php\">"; }else{ echo "文件上传失败,请稍等..."; echo "<meta http-equiv=\"refresh\" content=\"1; url=test.php\">"; } ?>