Home > Backend Development > PHP Tutorial > PHP file upload function - multiple file upload

PHP file upload function - multiple file upload

墨辰丷
Release: 2023-03-25 19:56:01
Original
2253 people have browsed it

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[&#39;PHP_SELF&#39;]?>" 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 = &#39;./&#39;.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[&#39;submit&#39;])){
        $info = &#39;&#39;;
        $count = count($_FILES[&#39;upload_file&#39;][&#39;name&#39;]);
        for($i=0; $i<$count; ++$i){
            if($_FILES[&#39;upload_file&#39;][&#39;name&#39;][$i] == "")
                continue;
            $info = upload(
                $_FILES[&#39;upload_file&#39;][&#39;error&#39;][$i],
                $_FILES[&#39;upload_file&#39;][&#39;tmp_name&#39;][$i],
                $_FILES[&#39;upload_file&#39;][&#39;name&#39;][$i]
            );
        }
        echo $info;
    }

?>
Copy after login

----- -------------------------------------------------- ----------------------------------
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template