weixin_33721344 2016-02-28 22:05 采纳率: 0%
浏览 60

AJAX图片上传参数

I have here a Simple Ajax Image Upload and i try to send over the Ajax Form another Parameter but i cant figure out how.

$("#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
    });

});

My Form

 <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>

And the 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';
}
?>

How can i now send over the Form another Parameter ? Maybe with a hidden input? Thanks for every Answer

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用lstm来预测股票价格
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥170 如图所示配置eNSP
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上