Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Posting an Image to PHP


This question is answered.


Permlink Replies: 9 - Last Post: Feb 28, 2017 9:28 AM Last Post By: Antonio Estevez
Jack Breon

Posts: 3
Registered: 3/18/14
Posting an Image to PHP  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 13, 2015 9:40 AM
Hi,

Can anyone help me with an example of how to post an image file to PHP? I have read about how to accomplish this and see form examples in HTML but can't find anything as to how I might use C++ Builder (XE8) to do the actual post of a binary. I need to be able to do this from an iOS app. Looking at Indy I don't know what I would use for that. I am mainly interested in the how to perform the post from C++ Builder as I think I understand the receiving PHP.

Thanks,
Jack

Here are a few things I found that tell how to do it. I just don't understand how to properly format the C++ to use TMultipartFormData and NetHTTPRequest. I can seem to construct the "NetHTTPRequest1->Post("http://www.example.com/rest/items", String1, NULL, TEncoding::Default);" by using the TMultipartFormData in the second param as it indicates. I think I am having issues here now because I don't understand how to accomplish it in C++. The second param wants a source but I don't understand why it indicates using TMultipartFormData since it isn't a valid param for the second parameter.

http://docwiki.embarcadero.com/RADStudio/XE8/en/Using_an_HTTP_Client
http://docwiki.embarcadero.com/Libraries/XE8/en/System.Net.HttpClientComponent.TNetHTTPRequest.Post
http://docwiki.embarcadero.com/Libraries/XE8/en/System.Net.Mime.TMultipartFormData_Methods
System.Net.Mime.TMultipartFormData.Create

Edited by: Jack Breon on May 13, 2015 11:17 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Posting an Image to PHP [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 13, 2015 11:59 AM   in response to: Jack Breon in response to: Jack Breon
Jack wrote:

Looking at Indy I don't know what I would use for that.

The TIdHTTP component, in particular its Put(), Post(TStream*), or Post(TIdMultipartFormDataStream*)
method, depending on what the PHP script is expecting for input.

Here are a few things I found that tell how to do it. I just don't
understand how to properly format the C++ to use TMultipartFormData
and NetHTTPRequest.

What exactly do you not understand?

The second param wants a source but I don't understand why it
indicates using TMultipartFormData since it isn't a valid param for
the second parameter.

Yes it is, but Post() wants it to be passed by pointer:

_di_IHTTPResponse __fastcall Post(..., TMultipartFormData* const ASource, 
...);


Not passed by value, which you seem to think it does. TMultipartFormData
is a TObject descendant, as as such it can never be instantiated on the stack,
only on the heap.

System.Net.Mime.TMultipartFormData.Create

Create() is a Delphi-style constructor. You would simply call "new TMultipartFormData"
in C++, like any other class.

--
Remy Lebeau (TeamB)
Jack Breon

Posts: 3
Registered: 3/18/14
Re: Posting an Image to PHP [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 13, 2015 2:09 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Jack wrote:

Looking at Indy I don't know what I would use for that.

The TIdHTTP component, in particular its Put(), Post(TStream*), or Post(TIdMultipartFormDataStream*)
method, depending on what the PHP script is expecting for input.

Here are a few things I found that tell how to do it. I just don't
understand how to properly format the C++ to use TMultipartFormData
and NetHTTPRequest.

What exactly do you not understand?

The second param wants a source but I don't understand why it
indicates using TMultipartFormData since it isn't a valid param for
the second parameter.

Yes it is, but Post() wants it to be passed by pointer:

_di_IHTTPResponse __fastcall Post(..., TMultipartFormData* const ASource, 
...);


Not passed by value, which you seem to think it does. TMultipartFormData
is a TObject descendant, as as such it can never be instantiated on the stack,
only on the heap.

System.Net.Mime.TMultipartFormData.Create

Create() is a Delphi-style constructor. You would simply call "new TMultipartFormData"
in C++, like any other class.

--
Remy Lebeau (TeamB)

I don't understand the ASource. This is probably basic stuff but some how I've been able to get along until now for the most part. I find it very embarrassing that I can't even seem to for the two lines to get started. Perhaps these two lines will display what I'm not understanding in addition to what you have already explained.

TMultipartFormData *MultipartFormData1 = new TMultipartFormData(this);

NetHTTPRequest1->Post("https://www.test.net/cgi-bin/testthepost.php",
"test",TMultipartFormData* const ASource,TEncoding::Default);

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Posting an Image to PHP [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 13, 2015 2:29 PM   in response to: Jack Breon in response to: Jack Breon
Jack wrote:

TMultipartFormData *MultipartFormData1 = new TMultipartFormData(this);

NetHTTPRequest1->Post("https://www.test.net/cgi-bin/testthepost.php",
"test",TMultipartFormData* const ASource,TEncoding::Default);

Should be more like this instead:

TMultipartFormData *MultipartFormData1 = new TMultipartFormData();
 
// populate MultipartFormData1 as needed, eg:
// MultipartFormData1->AddFile("fieldname", "path\\to\\filename.jpg");
 
NetHTTPRequest1->Post("https://www.test.net/cgi-bin/testthepost.php", MultipartFormData1);


--
Remy Lebeau (TeamB)
Jack Breon

Posts: 3
Registered: 3/18/14
Re: Posting an Image to PHP [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 13, 2015 2:48 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Jack wrote:

TMultipartFormData *MultipartFormData1 = new TMultipartFormData(this);

NetHTTPRequest1->Post("https://www.test.net/cgi-bin/testthepost.php",
"test",TMultipartFormData* const ASource,TEncoding::Default);

Should be more like this instead:

TMultipartFormData *MultipartFormData1 = new TMultipartFormData();
 
// populate MultipartFormData1 as needed, eg:
// MultipartFormData1->AddFile("fieldname", "path\\to\\filename.jpg");
 
NetHTTPRequest1->Post("https://www.test.net/cgi-bin/testthepost.php", MultipartFormData1);


--
Remy Lebeau (TeamB)

Thank you so much for getting me moving again! I'm so glad your around Remy!
K Y Iyer

Posts: 3
Registered: 8/11/09
Re: Posting an Image to PHP  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 23, 2017 10:48 PM   in response to: Jack Breon in response to: Jack Breon
Hi

C++ Builder 10 Seattle

I am unable to post a login form to my web server.
The php script receives blanks.
	TMultipartFormData *formData = new TMultipartFormData(false);
	formData->AddField("username", eUser->Text);
	formData->AddField("password", ePassword->Text);
	xRequest->URL = "http://xx.xx.xx.xx/userauth.php";
	xRequest->Post("http://xx.xx.xx.xx/userauth.php", formData);
	xRequest->Execute();


In PHP

<?php
	//header('Content-Type: application/text');
	echo $_POST["username"] . "/" . $_POST["password"];
?>


1. What am I doing wrong? Why am I not getting any values for username and password indexed parameters in PHP?
2. What is the boolean parameter to the constructor of TMultipartFormData?

The documentation has this line that says __fastcall TMultipartFormData(bool AOwnsStream);

What is AOwnsStream?

Many thanks in advance

Iyer


Jack Breon wrote:
Hi,

Can anyone help me with an example of how to post an image file to PHP? I have read about how to accomplish this and see form examples in HTML but can't find anything as to how I might use C++ Builder (XE8) to do the actual post of a binary. I need to be able to do this from an iOS app. Looking at Indy I don't know what I would use for that. I am mainly interested in the how to perform the post from C++ Builder as I think I understand the receiving PHP.

Thanks,
Jack

Here are a few things I found that tell how to do it. I just don't understand how to properly format the C++ to use TMultipartFormData and NetHTTPRequest. I can seem to construct the "NetHTTPRequest1->Post("http://www.example.com/rest/items", String1, NULL, TEncoding::Default);" by using the TMultipartFormData in the second param as it indicates. I think I am having issues here now because I don't understand how to accomplish it in C++. The second param wants a source but I don't understand why it indicates using TMultipartFormData since it isn't a valid param for the second parameter.

http://docwiki.embarcadero.com/RADStudio/XE8/en/Using_an_HTTP_Client
http://docwiki.embarcadero.com/Libraries/XE8/en/System.Net.HttpClientComponent.TNetHTTPRequest.Post
http://docwiki.embarcadero.com/Libraries/XE8/en/System.Net.Mime.TMultipartFormData_Methods
System.Net.Mime.TMultipartFormData.Create

Edited by: Jack Breon on May 13, 2015 11:17 AM

Edited by: K Y Iyer on Feb 23, 2017 10:48 PM
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Posting an Image to PHP [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 24, 2017 4:42 AM   in response to: K Y Iyer in response to: K Y Iyer
El 24/02/2017 a las 07:48, K Y Iyer escribió:
Hi

C++ Builder 10 Seattle

I am unable to post a login form to my web server.
The php script receives blanks.
	TMultipartFormData *formData = new TMultipartFormData(false);
	formData->AddField("username", eUser->Text);
	formData->AddField("password", ePassword->Text);
	xRequest->URL = "http://xx.xx.xx.xx/userauth.php";
	xRequest->Post("http://xx.xx.xx.xx/userauth.php", formData);
	xRequest->Execute();


In PHP

<?php
	//header('Content-Type: application/text');
	echo $_POST["username"] . "/" . $_POST["password"];
?>


1. What am I doing wrong? Why am I not getting any values for username and password indexed parameters in PHP?
2. What is the boolean parameter to the constructor of TMultipartFormData?

Try this:

    TStringList *Params= new TStringList();
    try
    {
       Params->Values[_D("username")]= eUser->Text;
       Params->Values[_D("password")]= ePassword->Text;
 
       _di_IHTTPResponse  Response= xRequest->Post(
           _D("http://xx.xx.xx.xx/userauth.php"), Params);
 
    }
    __finally
    {
       delete Params;
    }
K Y Iyer

Posts: 3
Registered: 8/11/09
Re: Posting an Image to PHP [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 27, 2017 11:44 PM   in response to: Antonio Estevez in response to: Antonio Estevez
Many thanks for helping.

Still not working

my PHP does not seem to be receiving anything
Why do I need to set the URL property of the httprequest object? If I don't set it, it get an invalid URL error.

		Params->Values[_D("username")]= eUser->Text;
		Params->Values[_D("password")]= ePassword->Text;
		xRequest->URL = _D("http://xx.xx.xx.xx/userauth.php");
		xRequest->Post(_D("http://xx.xx.xx.xx/userauth.php"), Params);
		xRequest->Execute();
 

Antonio Estevez wrote:
El 24/02/2017 a las 07:48, K Y Iyer escribió:
Hi

C++ Builder 10 Seattle

I am unable to post a login form to my web server.
The php script receives blanks.
	TMultipartFormData *formData = new TMultipartFormData(false);
	formData->AddField("username", eUser->Text);
	formData->AddField("password", ePassword->Text);
	xRequest->URL = "http://xx.xx.xx.xx/userauth.php";
	xRequest->Post("http://xx.xx.xx.xx/userauth.php", formData);
	xRequest->Execute();


In PHP

<?php
	//header('Content-Type: application/text');
	echo $_POST["username"] . "/" . $_POST["password"];
?>


1. What am I doing wrong? Why am I not getting any values for username and password indexed parameters in PHP?
2. What is the boolean parameter to the constructor of TMultipartFormData?

Try this:

    TStringList *Params= new TStringList();
    try
    {
       Params->Values[_D("username")]= eUser->Text;
       Params->Values[_D("password")]= ePassword->Text;
 
       String Response= xRequest->Post(
           _D("http://xx.xx.xx.xx/userauth.php"), Params);
 
    }
    __finally
    {
       delete Params;
    }
K Y Iyer

Posts: 3
Registered: 8/11/09
Re: Posting an Image to PHP [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2017 4:17 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Many thanks for helping.

Still not working

my PHP does not seem to be receiving anything
Why do I need to set the URL property of the httprequest object? If I don't set it, it get an invalid URL error.

		Params->Values[_D("username")]= eUser->Text;
		Params->Values[_D("password")]= ePassword->Text;
		xRequest->URL = _D("http://xx.xx.xx.xx/userauth.php");
		xRequest->Post(_D("http://xx.xx.xx.xx/userauth.php"), Params);
		xRequest->Execute();
 

Antonio Estevez wrote:
El 24/02/2017 a las 07:48, K Y Iyer escribió:
Hi

C++ Builder 10 Seattle

I am unable to post a login form to my web server.
The php script receives blanks.
	TMultipartFormData *formData = new TMultipartFormData(false);
	formData->AddField("username", eUser->Text);
	formData->AddField("password", ePassword->Text);
	xRequest->URL = "http://xx.xx.xx.xx/userauth.php";
	xRequest->Post("http://xx.xx.xx.xx/userauth.php", formData);
	xRequest->Execute();


In PHP

<?php
	//header('Content-Type: application/text');
	echo $_POST["username"] . "/" . $_POST["password"];
?>


1. What am I doing wrong? Why am I not getting any values for username and password indexed parameters in PHP?
2. What is the boolean parameter to the constructor of TMultipartFormData?

Try this:

    TStringList *Params= new TStringList();
    try
    {
       Params->Values[_D("username")]= eUser->Text;
       Params->Values[_D("password")]= ePassword->Text;
 
       String Response= xRequest->Post(
           _D("http://xx.xx.xx.xx/userauth.php"), Params);
 
    }
    __finally
    {
       delete Params;
    }
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Posting an Image to PHP [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2017 7:49 AM   in response to: K Y Iyer in response to: K Y Iyer
El 28/02/2017 a las 13:17, K Y Iyer escribió:
Many thanks for helping.

Still not working

my PHP does not seem to be receiving anything
Why do I need to set the URL property of the httprequest object? If I don't set it, it get an invalid URL error.

		Params->Values[_D("username")]= eUser->Text;
		Params->Values[_D("password")]= ePassword->Text;
		xRequest->URL = _D("http://xx.xx.xx.xx/userauth.php");
		xRequest->Post(_D("http://xx.xx.xx.xx/userauth.php"), Params);
		xRequest->Execute();
 

Here you have a complete example:

#include <System.Net.HttpClient.hpp>
// ...
 
    try
    {
       THTTPClient *Client= THTTPClient::Create();
       try
       {
          TStringList *Params= new TStringList();
          try
          {
             Params->Values[_D("username")]= eUser->Text;
             Params->Values[_D("password")]= ePassword->Text;
 
             _di_IHTTPResponse Response= Client->Post(_D("http://xx.xx.xx.xx/userauth.php"), Params);
 
             if (Response->StatusCode== 200)
             {
                String Content= Response->ContentAsString();
                // Use Content as needed
 
             }
             else
                throw Exception(_D("Server error: ") + Response->StatusText);
          }
          __finally
          {
             delete Params;
          }
       }
       __finally
       {
          delete Client;
       }
    }
    catch (const Exception &E)
    {
       // Show E.Message to the user
    }
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02