0

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.

Community
  • 1
  • 1
PHPDev
  • 81
  • 5
  • 1
    If you are actually seeing `[object FileList]` in the network panel, then that's most likely the problem. How that ends up there, we can hardly tell with only partial code. Please provide a [mcve]. – CBroe May 16 '16 at 09:52
  • When i traverse through the form vars on the request, i have a function that checks all the inputs. It adds to an object array. The call for a file is values[input_name] = inputs[i].files; So i end up with params{name: fileList} – PHPDev May 16 '16 at 09:57
  • I just checked the debugger, and when it goes through `values[input_name] = inputs[i].files` it loads fileList into the object. When i trace through the initial traverse of the input, right at the very first instance of the input, i can see the files>file>all attributes of the file in the input object. – PHPDev May 16 '16 at 10:05
  • `values[input_name] = inputs[i].files[0] changes the payload as i have edited above, but still get empty array() in REQUEST and FILES. – PHPDev May 16 '16 at 10:26
  • If i change the contentType to `application/x-www-form-urlencoded` it spits the payload out to the $_REQUEST array in one var and displays it in print_r. Yet will not populate the REQUEST or FILES arrays in PHP. – PHPDev May 16 '16 at 10:29
  • Why are you adding the inputs individually? Why not just `var formParams = new FormData(your_form);` and be done with it? – Quentin May 16 '16 at 10:29
  • Because this is a site wide function that works on thousands of other AJAX calls, and dozens of forms. The forms process through a Request Parser and are turned into Object arrays of key=value. The Request Parser does quite a few things, but in this case is minimal. It creates an Object Array of all form data, some of that data is manipulated before it is sent. This works on other forms fine, it is just these Files that are screwing it up. – PHPDev May 16 '16 at 10:37
  • The form sends fine without the images, and without multipart. As soon as i change to multipart, it doesnt want to work. As above, if the params are a string, no probs, as soon as they are an object, lots of problems. – PHPDev May 16 '16 at 10:40

0 Answers0