将数据+文件发布到PHP

时间:2015-07-27 19:33:21

标签: php jquery ajax file-upload

我正在尝试将一些图像和数据发布到PHP页面,但是我很难让它工作。在我看来,我使用的是Uploading both data and files in one form using Ajax?中提到的相同方法,但我无法使其工作。 UploadPictures()成功函数中的警报只返回空白。这是相关的HTML:

<div class="UploadDeletePicturesDIV">
    <form id="UploadDeletePicturesFORM" method="post" enctype="multipart/form-data">
        <a class="Button" id="DeleteFrontPageGalleryButton" onClick="DeletePicture(document.getElementById('FrontPageGalleryPicture').src, 'frontpagegallery')">Verwijderen</a><br>
        <input type="file" id="UploadPictures" name="UploadPictures" multiple><br>
        <input type="hidden" name="Adapter" value="Pictures">
        <input type="hidden" name="Call" value="UploadPictures">
        <input type="hidden" name="PictureFolder" value="frontpagegallery">
        <a class="Button" id="UploadFrontPageGalleryButton" onClick="UploadFrontPageGalleryPictures()">Uploaden</a>
    </form>
</div>

相关的javascript:

function UploadFrontPageGalleryPictures() {
    var form = $('#UploadDeletePicturesFORM');
    var filedata = new FormData();
    var i;
    jQuery.each($('#UploadPictures')[0].files, function(i, file) {
        filedata.append('Picture_'+i, file);
    });
    var other_data = form.serializeArray();
    $.each(other_data,function(key,input){
        filedata.append(input.name,input.value);
    });
    $.ajax({
        url: '/functions/functions.php',
        type: 'POST', 
        contentType: false,
        processData: false,
        data: filedata,
        success: function(data) {
            alert(data);
        },
        error: function(e) {
            alert(e);
        }
    }); 
}

相关的php:

<?php $pictures_basedir = "/home/deb95781/domains/blabla.com/public_html/images/";
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH']) ){
    if( $_POST['Adapter'] == 'Pictures' ){
        echo 'Adapter = ' . $_POST['Adapter'] . ' ';
        echo 'Call = ' . $_POST['Call'] . ' ';
        echo 'PictureFolder = ' . $_POST['PictureFolder'];
        if($_POST['Call'] == 'UploadPictures') {
            if(isset($_POST['PictureFolder'])) {
                global $pictures_basedir;
                $x = 0;
                echo "x: " . x;
                echo "name: " . $_FILES["Picture_" . $x]["name"];
                while($_FILES["Picture_" . $x]["name"] <> ""){
                    $target_dir = $pictures_basedir . $_POST['PictureFolder'] . '/' . basename( $_FILES["Picture_" . $x]["name"]);
                    $uploadOk = true;

                    if (file_exists($target_dir . $_FILES["Picture_" . $x]["name"])) {
                        echo "0___Sorry, foto " . $_FILES["Picture_" . $x]["name"] . " bestaat al./n";
                        $uploadOk = false;
                    }

                    if ($uploadOk) {
                        if (move_uploaded_file($_FILES["Picture_" . $x]["tmp_name"], $target_dir)) {
                            echo "1___Foto ". basename( $_FILES["Picture_" . $x]["name"]). " is geüpload./n";
                            //if it was an edit, remove the original file
                        } else {
                            echo "0___Sorry, er is iets foutgelopen bij het uploaden van foto " . $_FILES["Picture_" . $x]["name"] . "./n";
                        }
                    }

                    $x = $x+1;
                }
            }
        } 
    }
} ?>

我首先尝试不使用

将隐藏的输入添加到FormData()对象
data: 'Adapter=Pictures&Call=UploadPictures&PictureFolder=frontpagegallery&' + filedata,

但这并不是一个巨大的成功,这就是我将所有内容添加到表单中的原因。

0 个答案:

没有答案