dtypj3308 2016-05-16 09:45
浏览 33

$ _REQUEST和$ _FILES没有填充multipart / form-data

I have an ajax call bring up a form, this ajax call is successful using a string of variable var=value&var2=value2 etc. So i know that the AJAX Response is working.

Once the form is loaded into the AJAX Response it is parsed for any Javascript that needs to be run. I have an applet that I am building that modifies the form to upload four images.

$inRetailImages = "<script>ImgShower.init()</script>";
$inRetailImages .= "<div id=\"retailimagescontainer\"></div>";

This parse fine and loads the Javascript applet which is then used to load four images into it. When loading the four images, it modifies the form with an input for each image. I see this happening in the browsers debugger.

input = document.createElement('input');
input.id= "addShowerImage" + id;
input.name= 'addShowerImage' + id;
input.type='file';
input.size='40';
input.runat = 'server';
input.style.display = 'none';
input.accept='image/jpeg';
input.onchange = function(){ ImgShower.select(id); }
formparent.appendChild(input);

This appends the input to the current form. This is working, because when i submit the form without the images using a string rather than a form object, the form recognises the inputs although they are not valid images, when submitting the AJAX call via a string of vars, it takes the value of the local file directory, which is obviously not what is needed, but shows the AJAX call is working and the process script for the PHP is working. This form works perfectly when not uploading images.

if(isString(params)){
    params = params + '&mode=' + mode;
    contenttype = 'application/x-www-form-urlencoded';
}
else if (isObject(params)){
    params.mode = mode;
    queryparams = Request.getQueryParamsAsObject(params.params);
    Objects.merge(params, queryparams);
    delete params["params"];

    var formParams = new FormData();
    for (key in params) {
        formParams.append(key, params[key]);
        log("error", "FormData Key: " + key + " Data:" + params[key]);
    }
    params = formParams;
    //contenttype = "multipart/form-data;Charset=UTF-8;boundary=xxxxx;";
    //contenttype = 'application/x-www-form-urlencoded';

    EDIT: I CHANGED THE CONTENT TYPE TO FALSE
    contenttype = false;
    AND PUT AN IF STATEMENT TO PUT NO HEADER IN THE AJAX SEND
    AND EDITED 
    formParams.append(key, params[key] || "");
    SO THAT THE PASSED VALUES WERE NOT EMPTY AND THE FORM SENDS
    IT STILL WONT PASS AN IMAGE FILE TO $_FILES BUT THIS MADE IT FILL $_REQUEST
}

I have a script that takes all the value from the form and turns it into an Object Array. In the debugger i can see all the values from the form as expected including the FileItem for each of the four images. When view the log from the script above loading it into the formData object, i can see the valid entries and they all look good.

I have tried multiple content types, i have tried sending just the object without the form data, i have tried adding a new form wrapper to the form data, i have tried naming a new form wrapper for the formData object, i have looked through my PHP settings, and gone through this list.

Why would $_FILES be empty when uploading files to PHP?

The list of to watch out for is pretty extensive and my php.ini settings seem fine.

When i submit the form using application/x-www-form-urlencoded i see a large multipart in the response, but it does not populate $_REQUEST, $_POST or $_FILES as one would excpect. It loads the $_REQUEST with one variable and the multipart data.

When i submit the form using multipart/form-data, i see the Response in Googles Network Tab as a Payload being sent to the PHP Server, but at the PHP end it does not populate $_REQUEST, $_POST or $_FILES at all.

This is what i see in the Payload of the Request and Response on Google Chromes Network Tab of the Developer Tools.

------WebKitFormBoundaryp39DDe2JV8jjxA5j
Content-Disposition: form-data; name="addRetailName"

asdasd
------WebKitFormBoundaryp39DDe2JV8jjxA5j
Content-Disposition: form-data; name="addRetailCode"

TEST001
------WebKitFormBoundaryp39DDe2JV8jjxA5j
Content-Disposition: form-data; name="addRetailManuf"

etc for 34 fields

This form has 34 fields, + the 4 added fields using the javascript to update the form. These images can be seen in the Payload as well.

------WebKitFormBoundaryzmu5mDvaLNnvsolE
Content-Disposition: form-data; name="addShowerImage1"

[object FileList]

Why does this not populate the $_REQUEST and $_FILES arrays. I output the arrays in the PHP using print_r($_REQUEST) or print_r($_FILES) and get array() as the response.

I am not getting any files coming through, nor am i getting any form data coming through at all. Yet i see the multipart being sent in the Payload of the Request.

I am using pure javascript with no libraries and no jscript. I prefer to write in pure javascript.

I should also state, that i am on a windows 7 ultimate machine using IIS 7 and PHP 5.4. via FastCGI.

When calling the ajax, the form is passed and collected by a request function. This function scrolls through all the items of the form and collects their values or contents. This works for all text based content.

I call all file inputs

var inputs  = formObj.getElementsByTagName("input");

then traverse through those inputs

case "file":
    values[input_name] = inputs[i].files;
break;  
return values;

I even tried

values[input_name] = inputs[i].files[0]; returns File and not FileList

and i get this in the Payload

------WebKitFormBoundaryqsinSx1UoPEKAYOD
Content-Disposition: form-data; name="addShowerImage1";
filename="13006682_10208925038300663_5601989752145619827_n.jpg"
Content-Type: image/jpeg

This is then an Object Array

values{var1: value1, filevar: File}

It seems the FileList/File is appearing here. This is at the very beginning of recieving the form submit, the function traversing all the form values sees the files>fileList. I dont see anything other than fileList.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况