esp32 post multipart/form-data: Undefined index error [SOLVED]

i’ve been trying to POST an SPIFFS file from esp32 to my local server…
but i cannot succeed…
i have seen numerous other posts doing multipart POST and compare to my code, and i cannot find any problem…(i use the info here)
(of course i have tested simple connections (some GET etc…and everything looks to work)

this is my esp32 code:

    const char* localhost = "192.168.1.X";
    writeFile(SPIFFS, "/001.txt", "this is a test");
    file ="/001.txt");
    int myFileSizeInt = file.size();
    String myFileSizeStr = (String)myFileSizeInt;
    client.print(String("POST ") + "/save_File.php" +" HTTP/1.1\r\n" +
                 "Host: " + localhost + "\r\n" +
                 "Content-Type: multipart/form-data;boundary=boundary\r\n" +
                 "Content-Length: " + myFileSizeStr + "\r\n\r\n" +
                 "--boundary\r\n" +
                 "Content-Disposition: form-data; name=\"userfile\"; filename=\"001.txt\"\r\n" +
                 "Content-Type: text/plain\r\n\r\n");

while (file.available()) {
      char c =;


    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
    Serial.println("localhost response");
    while (client.available()) {
      char c =;

my save_File.php is like example 2 here

But i receive:

Undefined index: userfile in <b>myphpFolder..</b> on line <b>7</b>


<b>Notice</b>:  Undefined index: userfile in <b>myphpFolder..</b> on line <b>11</b>

Possible file upload attack!
Here is some more debugging info:Array
</pre>HTTP/1.1 400 Bad Request

any idea?


You have to be very careful and include in your content-length EVERYTHING that is in your body (a.k.a after the header empty line…in this example after “Content-Type: text/plain\r\n\r\n”)
when i say everything i mean boundary strings, file size, content-disposition things…etc
i found this and this very helpful