欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php jquery 多文件上传简单实例

程序员文章站 2022-06-14 16:38:47
复制代码 代码如下:
upload
 

php jquery 多文件上传简单实例

复制代码 代码如下:

<div id="mulitplefileuploader">upload</div> 

<div id="status"></div> 
<script> 

$(document).ready(function() 


var settings = { 
    url: "upload.php", 
    method: "post", 
    allowedtypes:"jpg,png,gif,doc,pdf,zip", 
    filename: "myfile", 
    multiple: true, 
    onsuccess:function(files,data,xhr) 
    { 
        $("#status").html("<font color='green'>upload is success</font>"); 

    }, 
    onerror: function(files,status,errmsg) 
    {        
        $("#status").html("<font color='red'>upload is failed</font>"); 
    } 

$("#mulitplefileuploader").uploadfile(settings); 

}); 
</script> 

upload.php

复制代码 代码如下:

<?php 
//if directory doesnot exists create it. 
$output_dir = "../upload"; 

if(isset($_files["myfile"])) 

    $ret = array(); 

    $error =$_files["myfile"]["error"]; 
   { 

        if(!is_array($_files["myfile"]['name'])) //single file 
        { 
            $filename = $_files["myfile"]["name"]; 
            move_uploaded_file($_files["myfile"]["tmp_name"],$output_dir. $_files["myfile"]["name"]); 
             //echo "<br> error: ".$_files["myfile"]["error"]; 

                 $ret[$filename]= $output_dir.$filename; 
        } 
        else 
        { 
                $filecount = count($_files["myfile"]['name']); 
              for($i=0; $i < $filecount; $i++) 
              { 
                $filename = $_files["myfile"]["name"][$i]; 
                 $ret[$filename]= $output_dir.$filename; 
                move_uploaded_file($_files["myfile"]["tmp_name"][$i],$output_dir.$filename ); 
              } 

        } 
    } 
    echo json_encode($ret); 



?>