AJAX Image Uplaod参数

时间:2016-02-28 22:05:54

标签: javascript php jquery ajax

我这里有一个简单的Ajax图像上传,我尝试通过Ajax表单发送另一个参数,但我无法弄清楚如何。

$("#uploadBTN").click(function(event) {
    event.preventDefault();
    var tmp_form = $("#fileinfo")[0];
    var fd = new FormData(tmp_form);
    $.ajax({
        url: './ajax/image_post_upload.php',  
        type: 'POST',
        data: fd,
        async: true,
        success:function(data){
            $('#output').html(data);
        },
        cache: false,
        contentType: false,
        processData: false
    });

});

我的表格

 <form id="fileinfo">
<input style="float:left;" id="fileinfo" name="userImage" type="file" class="file" />
<div  id="uploadBTN" class="file_boxes_buttons">Hochladen</div>
<div style="float:right; margin-left:10px;" id="output">LOG</div>
</form>

和PHP

<?php
session_start();

if(isset($_FILES['file'])){
    $filename = $_FILES['file']['name'];
    if(isset($_FILES['file']['name']) && !empty($filename)){        

        $target_dir = "./UPLOADS/";
        $target_file = $target_dir . basename($_FILES["file"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image
        if(isset($_POST["submit"])) {
            $check = getimagesize($_FILES["file"]["tmp_name"]);
            if($check !== false) {
                echo "File is an image - " . $check["mime"] . ".";
                $uploadOk = 1;
            } else {
                echo "File is not an image.";
                $uploadOk = 0;
            }
        }
        // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["file"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
        } else {
            $root = getcwd();
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $root.$target_file)) {
                echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }














    }else{
        echo 'please choose a file';
    }
}else{
    echo 'not set';
}
?>

我现在如何通过Form发送另一个参数? 也许隐藏输入? 谢谢你的每一个答案

0 个答案:

没有答案