I tested the PHP script by uploading a file via a html form, and it seems to work ok.
Here is the PHP script:
Code: Select all
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the uploaded file information
$file = $_FILES["filename"];
$fileName = $file["name"];
$fileTmpName = $file["tmp_name"];
$fileError = $file["error"];
if ($fileError === UPLOAD_ERR_OK) {
// Get the uploader's IP address
$uploaderIP = $_SERVER["REMOTE_ADDR"];
// Generate a new filename with the uploader's IP and current date/time
$newFileName = $uploaderIP . "_" . date("YmdHis") . "_" . $fileName;
error_log($newFileName);
// Specify the directory to save the file
$uploadDirectory = "/kunden/homepages/8/d963708782/htdocs/Data/sounds/";
// Move the uploaded file to the destination directory with the new filename
if (move_uploaded_file($fileTmpName, $uploadDirectory . $newFileName)) {
echo "File uploaded successfully!";
error_log("uploaded file");
} else {
echo "Error uploading file.";
error_log("Error uploading file");
}
} else {
echo "Error: " . $fileError;
}
}
?>
Code: Select all
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
switch(evt->event_id)
{
case HTTP_EVENT_ERROR:
ESP_LOGE(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
break;
case HTTP_EVENT_REDIRECT:
ESP_LOGI(TAG, "HTTP_EVENT_REDIRECT");
break;
}
return ESP_OK;
}
void http_task(void *args)
{
// Set up HTTP client
esp_err_t err;
esp_http_client_config_t config = {
.url = SERVER_URL,
.method = HTTP_METHOD_POST,
.event_handler = _http_event_handler,
.keep_alive_enable = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
//esp_http_client_set_header(client, "Transfer-Encoding", "chunked");
// Open file for reading
char fname[32];
char header_fname[32];
char header_size[16];
FILE *file;
strcpy(fname,"/sdcard/FILE.TXT");
strcpy(header_fname,"FILE.TXT");
file= fopen(fname, "r");
if(file==NULL)
{
ESP_LOGE(TAG,"error opening file");
}
else
{
}
// Get file size
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
ESP_LOGI(TAG, "File size: %ld bytes", file_size);
// POST
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_header(client, "Content-Type", "application/octet-stream");
itoa(file_size,header_size,10);
ESP_LOGI(TAG,"file size for header is %s",header_size);
esp_http_client_set_header(client, "Content-Length", header_size);
esp_http_client_set_header(client, "filename", header_fname);
// char content_disposition_header[64];
// esp_http_client_set_header(client, "Content-Disposition", content_disposition_header);
err = esp_http_client_open(client, file_size);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
esp_http_client_cleanup(client);
return;
}
/*
err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %lld",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}
*/
// Send file in chunks
size_t total_sent = 0;
char data[CHUNK_SIZE];
#if 1
while (total_sent < file_size)
{
size_t to_send = CHUNK_SIZE;
if (total_sent + to_send > file_size) {
to_send = file_size - total_sent;
}
fread(data, 1, to_send, file);
ESP_LOGI(TAG,"Read %d bytes, sending now",to_send);
int write_len = esp_http_client_write(client, &data[0], file_size);
if (write_len <= 0) {
ESP_LOGE(TAG, "Error occurred during file write: %s", esp_err_to_name(write_len));
esp_http_client_cleanup(client);
return;
}
err = esp_http_client_perform(client);
if (err != ESP_OK) {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}
total_sent += to_send;
}
#endif
// Clean up
fclose(file);
esp_http_client_cleanup(client);
ESP_LOGI(TAG, "File upload complete");
while(1)
{
vTaskDelay(100);
}
vTaskDelete(NULL);
}
I (2936) HTTP: File size: 49 bytes
I (2936) HTTP: file size for header is 49
I (3056) HTTP: HTTP_EVENT_ON_CONNECTED
I (3066) HTTP: HTTP_EVENT_HEADER_SENT
I (3066) HTTP: Read 49 bytes, sending now
I (3206) HTTP: HTTP_EVENT_ON_HEADER, key=Content-Type, value=text/html; charset=UTF-8
I (3206) HTTP: HTTP_EVENT_ON_HEADER, key=Transfer-Encoding, value=chunked
I (3206) HTTP: HTTP_EVENT_ON_HEADER, key=Connection, value=keep-alive
I (3216) HTTP: HTTP_EVENT_ON_HEADER, key=Keep-Alive, value=timeout=15
I (3226) HTTP: HTTP_EVENT_ON_HEADER, key=Date, value=Sun, 04 Jun 2023 22:03:05 GMT
I (3236) HTTP: HTTP_EVENT_ON_HEADER, key=Server, value=Apache
I (3236) HTTP: HTTP_EVENT_ON_DATA, len=8
I (3246) HTTP: HTTP_EVENT_ON_FINISH
I (3246) HTTP: HTTP_EVENT_DISCONNECTED
I (3256) HTTP: File upload complete
However, the php script never sees the filename , and therefore does not create the file, erroring out
Any idea of what am I missing?