php - How to convert the parameters of an HTML webform to use with TIdHTTP in Delphi?

How to convert the parameters of an HTML webform to use with TIdHTTP in Delphi?
I have a web server, with PHP, and a function to upload files. Via a web browser, I can send files without problems, but when I use TIdHTTP, it presents an error.
This is the HTML for the file upload page:
<html>
<head>
<title>Upload de Arquivos com PHP</title>
</head>
<body>
<form method="post" action="recebe_upload.php" enctype="multipart/form-data">
<label>Arquivo:</label>
<input type="file" name="arquivo" />
<input type="submit" value="Enviar" />
</form>
</body>
</html>
This is the procedure I used to send the files:
procedure TForm1.Button1Click(Sender: TObject);
var
Response, URL : String;
LHTTPClient : TIdHTTP;
Lista : TIdMultiPartFormDataStream;
server, script, caminhoarq : string;
begin
URL := 'http://192.168.15.101/RECEBE_UPLOAD.php\';
Lista := TIdMultiPartFormDataStream.Create;
HTTPClient := TIdHTTP.Create;
HTTPClient.ProtocolVersion := pv1_1;
HTTPClient.Request.ContentType := 'utf-8';
HTTPClient.Request.UserAgent := 'Mozilla/3.0 (compatible;Indy Library)';
HTTPClient.HTTPOptions := [hoForceEncodeParams, hoKeepOrigProtocol];
Response := '';
try
try
Lista.AddFormField('Arquivo','C:\Client\teste.txt');
Response := UTF8Decode(Trim(HTTPClient.Post(URL, Lista)));
memo1.Lines.add(response);
except
on e : exception do
ShowMessage('Erro ao enviar arquivo ao servidor! Detalhes: '+e.Message);
end;
finally
Lista.Free;
HTTPClient.free;
end;
end;
This is the error message I get:
<br />
<b>Notice</b>: Undefined index: arquivo in <b>C:\xampp\htdocs\RECEBE_UPLOAD.php</b> on line <b>22</b><br />
<br />
<b>Notice</b>: Undefined index: arquivo in <b>C:\xampp\htdocs\RECEBE_UPLOAD.php</b> on line <b>30</b><br />
<br />
<b>Strict Standards</b>: Only variables should be passed by reference in <b>C:\xampp\htdocs\RECEBE_UPLOAD.php</b> on line <b>30</b><br />
Answer
Solution:
You are not populating theTIdMultiPartFormDataStream
correctly. You need to use theAddFile()
method, not theAddFormField()
method. The latter is meant for text fields instead.
Also, PHP's$_POST
is case-sensitive when accessing webform fields. The HTML sends a field namedarquivo
, but your Delphi code is sending a field namedArquivo
instead. That is why the PHP error saysarquivo
is unknown.
Also, the URL you are posting to is wrong, and theContentType
you are setting is wrong (thoughPost()
will overwrite it).
Try this instead:
procedure TForm1.Button1Click(Sender: TObject);
var
Response, URL : String;
LHTTPClient : TIdHTTP;
Lista : TIdMultiPartFormDataStream;
begin
URL := 'http://192.168.15.101/recebe_upload.php';
Lista := TIdMultiPartFormDataStream.Create;
try
Lista.AddFile('arquivo', 'C:\Client\teste.txt');
HTTPClient := TIdHTTP.Create;
try
HTTPClient.ProtocolVersion := pv1_1;
HTTPClient.HTTPOptions := [hoForceEncodeParams, hoKeepOrigProtocol];
try
Response := Trim(HTTPClient.Post(URL, Lista));
Memo1.Lines.Add(Response);
except
on e : Exception do
ShowMessage('Erro ao enviar arquivo ao servidor! Detalhes: ' + e.Message);
end;
finally
HTTPClient.Free;
end;
finally
Lista.Free;
end;
end;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.