Home > Backend Development > PHP Tutorial > input tag file type, select multiple files to upload

input tag file type, select multiple files to upload

WBOY
Release: 2016-07-28 08:26:20
Original
1935 people have browsed it

html page

    <!DOCTYPE html>  
    <html>  
        <head>  
            <meta charset="UTF-8"/>  
            <title>xhr2</title>  
        </head>  
        <body>      
            <div style="text-align:center;margin:100px">        
                <input type="file" id="file" name="file" multiple="multiple">  
                <button /button>  
            </div>          
            <script>  
            function xhr2(){  
                var xhr = new XMLHttpRequest();//第一步  
                //定义表单变量  
                var file = document.getElementById('file').files;  
                //console.log(file.length);  
                //新建一个FormData对象  
                var formData = new FormData(); //++++++++++  
                //追加文件数据  
                for(i=0;i<file.length;i++){    
                     formData.append("file["+i+"]", file[i]); //++++++++++  
                }   
                //formData.append("file", file[0]); //++++++++++  
                  
                //post方式  
                xhr.open(&#39;POST&#39;, &#39;xhr2.php&#39;); //第二步骤  
                //发送请求  
                xhr.send(formData);  //第三步骤  
                //ajax返回  
                xhr.onreadystatechange = function(){ //第四步  
                if ( xhr.readyState == 4 && xhr.status == 200 ) {  
                  console.log( xhr.responseText );  
                }  
              };  
                //设置超时时间  
                xhr.timeout = 100000;  
                xhr.ontimeout = function(event){  
                alert(&#39;请求超时!&#39;);  
              }   
            }  
            </script>  
        </body>  
    </html>  
Copy after login

php processing page input标签file类型,选择多个文件进行上传

    <?php  
    print_r($_FILES["file"]);  
      
    for($i=0;$i<count($_FILES["file"][&#39;name&#39;]);$i++){  
    $name=$_FILES["file"]["name"][$i];  
    move_uploaded_file($_FILES["file"]["tmp_name"][$i],iconv("UTF-8","gb2312",$name));  
    }  
      
    ?>  

Copy after login

The above introduces the input tag file type, selecting multiple files for uploading, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.

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