时间:2021-07-01 10:21:17 帮助过:33人阅读
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="104857600" /> <table> <tr> <td>上传文件:<input name="upload_file[]" type="file" size="50" /></td> </tr> <tr> <td>上传文件:<input name="upload_file[]" type="file" size="50" /></td> </tr> <tr> <td>上传文件:<input name="upload_file[]" type="file" size="50" /></td> </tr> <tr> <td>上传文件:<input name="upload_file[]" type="file" size="50" /></td> </tr> <tr> <td><input type="submit" name="submit" value="上传"/></td> </tr> </table> </form> <?php function upload($file_error, $file_tmp_name, $file_name){ $info = ""; if($file_name == "") return $info; switch($file_error){ case UPLOAD_ERR_INI_SIZE: $info = $file_name. ": 文件大小超过了服务器的限制"; break; case UPLOAD_ERR_FORM_SIZE: $info = $file_name. ": 文件大小超过了浏览器的限制"; break; case UPLOAD_ERR_PARTIAL: $info = $file_name. ": 只上传了部分文件"; break; case UPLOAD_ERR_NO_FILE: $info = $file_name. ": 没有文件被上传"; break; case UPLOAD_ERR_NO_TMP_DIR: $info = $file_name. ": 找不到临时文件夹"; break; case UPLOAD_ERR_CANT_WRITE: $info = $file_name. ": 文件写入失败"; break; case UPLOAD_ERR_OK: $upload_dir = './'.iconv("UTF-8","gb2312",$file_name); if(file_exists($upload_dir)){ $info = $file_name.": 同名文件已经存在"; }else{ if(move_uploaded_file($file_tmp_name,$upload_dir)){ $info = $file_name.": 文件上传成功"; }else{ $info = $file_name.": 文件上传失败"; } } break; } return $info; } if(isset($_POST['submit'])){ $info = ''; $count = count($_FILES['upload_file']['name']); for($i=0; $i<$count; ++$i){ if($_FILES['upload_file']['name'][$i] == "") continue; $info = upload( $_FILES['upload_file']['error'][$i], $_FILES['upload_file']['tmp_name'][$i], $_FILES['upload_file']['name'][$i] ); } echo $info; } ?>
--------------------------------------------------------------------------------------
代码执行结果如下:
注意:
1、<input name="upload_file[]" type="file" size="50" />中, name="upload_file[]"一定要以数组形式命名,不然就会出现错误: “Uninitialized string offset: 0”,这句话的意思是你的数组key值越界了
2、$_FILES['upload_file']['name'][$i]中, upload_file是表单中上传文件标记符的名称,多文件上传时,数组$_FILES的第三维下标会自动从0开始依次编号。
相关推荐:
PHP文件上传功能实现代码分享
配置php.ini实现php文件上传功能
如何配置php.ini以实现PHP文件上传功能
以上就是PHP文件上传功能——多文件上传 的详细内容,更多请关注Gxl网其它相关文章!