Home >Backend Development >PHP Tutorial >PHP文件上传功能 多文件上传

PHP文件上传功能 多文件上传

WBOY
WBOYOriginal
2016-06-23 13:36:13926browse

这一节内容主要介绍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             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、中, name="upload_file[]"一定要以数组形式命名,不然就会出现错误:    “Uninitialized string offset: 0”,这句话的意思是你的数组key值越界了

2、$_FILES['upload_file']['name'][$i]中, upload_file是表单中上传文件标记符的名称,多文件上传时,数组$_FILES的第三维下标会自动从0开始依次编号。
Statement:
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