Jump to content
Luciano Veneziano

Upload files from Delphi to PHP

Recommended Posts

Hi everyone I have a PHP file that uploads images to me.
I should send with HTTPS and post the parameters from Delphi like this.

<form action = "UPLOAD.PHP" enctype = "multipart / form-data">
<input type = "file" name = "upfile" required />
<input type = "submit" value = "Upload" />
</form>

Unfortunately I'm not very good, I was able to make calls in https and post, but I could not set up idHTTP correctly

Has anyone already encountered this problem?

Thanks to all.

Share this post


Link to post
4 hours ago, Luciano Veneziano said:

I was able to make calls in https and post, but I could not set up idHTTP correctly

It should look like this:

uses
  ..., IdMultipartFormDataStream;

var
  PostData: TIdMultipartFormDataStream;
begin
  PostData := TIdMultipartFormDataStream.Create;
  try
    PostData.AddFile('upfile', 'C:\path\filename');
    IdHTTP.Post('https://server/UPLOAD.PHP', PostData);
  finally
    PostData.Free;
  end;
end;

 

Edited by Remy Lebeau

Share this post


Link to post

Funziona perfettamente:

 

function httpsUpload(URL : string; fname : String ) : String;
var
  LRequest: THTTPClient;
  LFormData: TMultipartFormData;
  LResponse: TStringStream;
begin
  LRequest := THTTPClient.Create;
  LFormData := TMultipartFormData.Create();
  LResponse := TStringStream.Create;
  try
    LFormData.AddField('json', '{"id":1,"user":"Luciano"}');   //optional
    LFormData.AddFile('file', fname);
    LRequest.Post(URL, LFormData, LResponse);
    Result := LResponse.DataString;
  finally
    LFormData.Free;
    LResponse.Free;
    LRequest.Free;
  end;
end;

 

phpfile

 

<?php
include_once("local/setup.php");
$where = $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $where))
{
  echo "OK\r\n";
}
else
{
  echo "ERR\r\n";
}
print_r($_FILES);
?>

Edited by Luciano Veneziano

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×