Arduino HTTP post image to external server

I'm trying to send an image from SPIFFS to my external server. When pictures are taken, they are saved into "/pics/" directory within SPIFFS. Ultimately, I'd like to post all the images saved onto "/pics/x.jpg" (x is the a number in increasing order - 7.jpg, 8.jpg, 9.jpg, etc...) to my external server as they come in. Currently, I can access all the images within the internal esp server. Right now, I'm trying to send a single image, "6.jpg" from "/pics/" to my external server. Unfortunately, I'm not seeing anything on the external server side. The http post code occurs in "void setup()". The most relevant Arduino code is below:

// Program header files

WiFiClient client;

void myCAMsaveToSPIFFS()
{
  // Reserves space when memory is full
  if((fileTotalKB - fileUsedKB) < fileSpaceOffset)
  {
    String maxStr = "========= Maximum Data Storage Reached =========";
    Serial.println(maxStr);
    errMsg = maxStr;
    return;
  }

  String str;

  // Flush the FIFO
  myCAM.flush_fifo();
  // Clear capture done flag
  myCAM.clear_fifo_flag();
  // Start capture
  myCAM.start_capture();

  Serial.println("Start capture.");
  while(!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK));
  Serial.println("Capture done!");

  // Increase file count since a capture was done
  fileCount++;

  len = myCAM.read_fifo_length();
  Serial.print("The FIFO length is: ");
  Serial.println(len, DEC);

  if(len >= MAX_FIFO_SIZE) // 8M
  {
    Serial.println("Over size!");
  }
  if(len == 0) // 0 KB
  {
    Serial.println("Size is 0 KB.");
  }

  str = "/pics/" + String(fileCount) + ".jpg";

  File f = SPIFFS.open(str, FILE_WRITE);
  if(!f)
  {
    Serial.println("Property file failed to open");
  }
  else
  {
    Serial.println(str);
  }

  i = 0;
  myCAM.CS_LOW();
  myCAM.set_fifo_burst();
  while (len--)
  {
    temp_last = temp;
    temp =  SPI.transfer(0x00);
    //Read JPEG data from FIFO
    if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
    {
      buf[i++] = temp;  //save the last  0XD9
      //Write the remain bytes in the buffer
      myCAM.CS_HIGH();
      f.write(buf, i);
      //Close the file
      f.close();
      Serial.println("CAM Save done!");
      is_header = false;
      i = 0;
    }
    if (is_header == true)
    {
      //Write image data to buffer if not full
      if (i < 256)
        buf[i++] = temp;
      else
      {
        //Write 256 bytes image data to file
        myCAM.CS_HIGH();
        f.write(buf, 256);
        i = 0;
        buf[i++] = temp;
        myCAM.CS_LOW();
        myCAM.set_fifo_burst();
      }
    }
    else if ((temp == 0xD8) & (temp_last == 0xFF))
    {
      is_header = true;
      buf[i++] = temp_last;
      buf[i++] = temp;
    }
  }

}


void camCapture(ArduCAM myCAM)
{
  WiFiClient client = server.client();

  len  = myCAM.read_fifo_length();
  if (len >= MAX_FIFO_SIZE) // 8M
  {
    Serial.println("Over size.");
  }
  if (len == 0 ) // 0KB
  {
    Serial.println("Size is 0.");
  }

  myCAM.CS_LOW();
  myCAM.set_fifo_burst();

  if (!client.connected()) return;

  i = 0;
  while ( len-- )
  {
    temp_last = temp;
    temp =  SPI.transfer(0x00);
    //Read JPEG data from FIFO
    if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
    {
      buffer[i++] = temp;  //save the last  0XD9     
      //Write the remain bytes in the buffer
      if (!client.connected()) break;
      client.write(&buffer[0], i);
      is_header = false;
      i = 0;
      myCAM.CS_HIGH();
      break; 
    }  
    if (is_header == true)
    { 
      //Write image data to buffer if not full
      if (i < bufferSize)
      {
      buffer[i++] = temp;
      }
    else
    {
      //Write bufferSize bytes image data to file
      if (!client.connected()) break;
      client.write(&buffer[0], bufferSize);
      i = 0;
      buffer[i++] = temp;
    }        
    }
    else if ((temp == 0xD8) & (temp_last == 0xFF))
    {
      is_header = true;
      buffer[i++] = temp_last;
      buffer[i++] = temp;   
    } 
  } 
}




void setup() 
{
  SPIFFS.begin();
  delay(10);

  Wire.begin();
  Serial.begin(115200);
  SPI.begin();
  SPI.setFrequency(4000000); //4MHz
  pinMode(CS, OUTPUT);

  pinMode(ArduCAM_Power, OUTPUT);
  digitalWrite(ArduCAM_Power, HIGH);
  

  if (wifiType == 0)
  {
    if(!strcmp(WIFI_SSID,"SSID"))
    {
      Serial.println(F("Please set your SSID"));
      while(1);
    }
    if(!strcmp(WIFI_PASSWORD,"PASSWORD"))
    {
      Serial.println(F("Please set your PASSWORD"));
      while(1);
    }
    // Connect to WiFi network
    Serial.println();
    Serial.println();
    Serial.print(F("Connecting to "));
    Serial.println(WIFI_SSID);

    WiFi.mode(WIFI_STA);

    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

    while (WiFi.status() != WL_CONNECTED) 
    {
      delay(500);
      Serial.print(F("."));
    }

    Serial.println(F("WiFi connected"));
    Serial.println("");
    Serial.println(WiFi.localIP());
  }
  

  File normPicture = SPIFFS.open("/pics/6.jpg", FILE_READ);
  uint32_t jpgLen = normPicture.size();

  Serial.print("Sending image of ");
  Serial.print(jpgLen, DEC);
  Serial.println("bytes"); 


  // Prepare request
  String start_request = "";
  String end_request = "";
  start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Type: image/jpeg" + "\n" + "Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"6.jpg\"\n" + "Content-Transfer-Encoding: binary\n\n";
  end_request = end_request + "\n--AaB03x--\n";  
  uint32_t extra_length;  
  extra_length = start_request.length() + normPicture.size() + end_request.length();  
  //Serial.println("Extra length:");  
  //Serial.println(extra_length);  
  long leng = jpgLen + extra_length;

  // Connect to server
  Serial.println("Starting connection to external server!");
  if (client.connect("172.xx.xx.x", 80)) 
  {  
    Serial.println("Connected");  
    client.print("POST /www/upload.php HTTP/1.1");  
    client.println("Host: 172.xx.xx.x" );  
    client.println("Content-Type: multipart/form-data; boundary=AaB03x");  
    client.print("Content-Length: ");  
    client.println(extra_length);  
    client.println();
    client.print(start_request);  
    Serial.println("Connected1");  

    while(normPicture.available())
    {
      client.write(normPicture.read());
    }

    Serial.println("END REQUEST");
    client.println(end_request);

  }
  else
  {
    Serial.println("Connection failed!");
  }

  // Setup handlers for internal server
  server.on("/capture", HTTP_GET, serverCapture);
  server.on("/stream", HTTP_GET, serverStream);
  server.on("/submit", handleSubmit);
  server.on("/clear", clearData);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("Internal Server started");

  File dir = SPIFFS.open("/pics");
  while(dir.openNextFile())
  {
    fileCount++;
  }

  fileTotalKB = (int)SPIFFS.totalBytes();
  fileUsedKB = (int)SPIFFS.usedBytes();

}

void loop() 
{
  server.handleClient();

}

My php code (upload.php) is below:

<?php
if (!empty($_FILES) && isset($_FILES['fileToUpload'])) {
    switch ($_FILES['fileToUpload']["error"]) {
        case UPLOAD_ERR_OK:
            $target = "upload/";
            $target = $target . basename($_FILES['fileToUpload']['name']);

            if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target)) {
                $status = "The file " . basename($_FILES['fileToUpload']['name']) . " has been uploaded";
                $imageFileType = pathinfo($target, PATHINFO_EXTENSION);
                $check = getimagesize($target);
                if ($check !== false) {
                    echo "File is an image - " . $check["mime"] . ".
";
                    $uploadOk = 1;
                } else {
                    echo "File is not an image.
";
                    $uploadOk = 0;
                }
            } else {
                $status = "Sorry, there was a problem uploading your file.";
            }
            break;
    }

    echo "Status: {$status}
\n";

}
?>

I don't believe that I need any html code, since the php file receives the image and sends it to the "uploads/" folder in "www/uploads/" from my external server. My upload.php file is located in "www/".

Thanks!

My only suggestion is to read the server response after your POST request. It will give you some insight into what is going wrong.

I think there is a small default upload size in most php.ini files, that made me think your server could be refusing that much data, but you'll have a much better idea of what is going on just by reading the HTTP response.

Your project seems really cool.

  start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Type: image/jpeg" + "\n" + "Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"6.jpg\"\n" + "Content-Transfer-Encoding: binary\n\n";
  end_request = end_request + "\n--AaB03x--\n";

In network protocols an end of line is usually "\r\n" and not just "\n".

directory/permissions stuff aside..

are you ever getting ANYTHING in the $_FILES object/array?