doujiu8479 2017-01-19 15:38
浏览 14

多文件扩展名验证

I have an html form where i have input file array. What im trying to achieve is get the file array in php file loop through it and check if a file extension is correct else add it to array and then show as json array in ajax. Can someone help me? here is the code for the form:

<html>
<head>
<title>Multiple file upload handling</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#form").submit(function(e){
        var file1 = $("#file1").val();
        var file2 = $("#file2").val();
        $("#results").text("");
        var results = $("#results");
        if(file1 == "" || file2 == ""){
            results.text("File 1 and File 2 are empty.");
        }else {
            var formarray = new FormData($("#form")[0]);
            $.ajax({
                url: "upload.php",
                type: "POST",
                dataType: "JSON",
                enctype: 'multipart/form-data',
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                data: formarray,
                success:function(response){
                    if(response.success == false){
                        $(response.message).each(function(index, value){    
                            results.append("<li id="+ index + ">" + value + "</li>");
                        });
                    }else{
                        alert(response.message);
                    }
                }
            });
        }   
        e.preventDefault();
    });
})
</script>
</head>
<body>
      <form method="post" role="form" enctype="multipart/form-data" id="form">
        <input type="file" id="file1" name="file[]" style="margin-bottom:30px;"><br>
        <input type="file" id="file2" name="file[]" style="margin-bottom:30px;"><br>
      <input type="submit" id="upload" name="upload">
      </form>
      <div id="results"></div>
      </body>
      </html>

And below is the php code:

<?php
    $filenames = $_FILES['file']['name'];
    $filesizes = $_FILES['file']['size'];
    $file_tmp = $_FILES['file']['tmp_name'];
    $data = array();
    $errors = array();
    foreach($filenames as $key=>$value){
        $filesize = $_FILES["file"]["size"][$key];
        $filetype = $_FILES["file"]["type"][$key];
        $file_tmpname = $_FILES["file"]["tmp_name"][$key];
        $valid_extensions = array("jpeg", "jpg", "gif", "png");
        $ext = explode(".", $value);
        $extension = end($ext);
        if(!in_array($extension, $valid_extensions)){
            $errors[] = $value;
            $data["success"] = false;
            $data["message"] = $errors;
        } else {
            $data["success"] = true;
            $data["message"] = "success";
            move_uploaded_file($file_tmpname, "uploads/".$filename);
        }   
    }
    echo json_encode($data);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度