This section mainly introduces the multi-file upload function of PHP upload files. As long as the file upload tag in the form is named in the form of an array, multiple files can be uploaded simultaneously.
Let’s take a look at an example:
---------------------------------- ----------------------------------
<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; } ?>
----- -------------------------------------------------- ----------------------------------
The code execution results are as follows:
Note:
1. In , name="upload_file[]" must be named in the form of an array. Otherwise, an error will occur: "Uninitialized string offset: 0", this sentence means that your array key value is out of bounds
2, $_FILES['upload_file']['name'][$i ], upload_file is the name of the upload file marker in the form. When uploading multiple files, the third-dimensional subscript of the array $_FILES will automatically be numbered sequentially starting from 0.
Related recommendations:
PHP file upload function to implement code sharing
Configure php.ini to implement PHP file upload function
How to configure php.ini to implement PHP file upload function
The above is the detailed content of PHP file upload function - multiple file upload. For more information, please follow other related articles on the PHP Chinese website!