esp32 post multipart/form-data: Undefined index error

uzer123
Posts: 12
Joined: Thu May 07, 2020 4:42 pm

esp32 post multipart/form-data: Undefined index error

Postby uzer123 » Thu May 07, 2020 5:55 pm

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:

Code: Select all

...
    const char* localhost = "192.168.1.X";
    writeFile(SPIFFS, "/001.txt", "this is a test");
    file = SPIFFS.open("/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 = file.read();
      client.print(c);
    }

client.print("\r\n--boundary--\r\n");

    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        Serial.println("headers received");
        break;
      }
    }
    Serial.println("localhost response");
    while (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
    client.stop();
  }
my save_File.php is like example 2 here


But i receive:

Code: Select all

Undefined index: userfile in <b>myphpFolder..</b> on line <b>7</b><br />
<pre><br />
<b>Notice</b>:  Undefined index: userfile in <b>myphpFolder..</b> on line <b>11</b><br />
Possible file upload attack!
Here is some more debugging info:Array
(
)
</pre>HTTP/1.1 400 Bad Request
...
any idea?

IntrepidUniverse
Posts: 5
Joined: Wed May 06, 2020 6:06 pm

Re: esp32 post multipart/form-data: Undefined index error

Postby IntrepidUniverse » Sat May 09, 2020 6:43 pm

Hello uzer123,

Take a look at this answer:
https://stackoverflow.com/questions/358 ... les-to-php

It mentions the empty $_FILES problem - your error message is showing an empty $_FILES array. It suggests a problem with your server configuration. We don't know what web server you are using (apache nginx etc) or if your php.ini settings are correct but maybe if you check the items mentioned above you can find out exactly where it is going wrong and fix your project.

uzer123
Posts: 12
Joined: Thu May 07, 2020 4:42 pm

Re: esp32 post multipart/form-data: Undefined index error

Postby uzer123 » Sun May 10, 2020 3:39 pm

Thanks for the answer..

i have solved the problem by myself so i'll post the solution.
There isnt any problem with my server (its apache using XAMPP by the way).
(i forgot to mention that i had already tested simple GET requests..and it worked fine..

then i tried simple POST requests with

Code: Select all

Content-Type: application/x-www-form-urlencoded;
and i was sending a simple txt (like the 001.txt from above) and i used a php like this:

Code: Select all

<?php print_r($_POST); ?>
Then i understood, that not all my txt message was sending..
Lets say, if the 001.txt was "i am a string", the server was getting "i am a s"

TL;DR: i used wrong Content-length because i didnt include

Code: Select all

"name="
and

Code: Select all

 filename
String length.

So i fixed it..and realized now, i did similar mistake with multipart/form-data POST.

The answer is:
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 http://chxo.com/be2/20050724_93bf.html and https://stackoverflow.com/questions/532 ... httpclient very helpful

Who is online

Users browsing this forum: No registered users and 34 guests