I need to send binary data from a Windows app. to a web site

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

I need to send binary data from a Windows app. to a web site

Post by bugmenot »

I need to send binary data from a Windows application to a site. I am used with compiled programming but I don't know much PHP :banghead: :mrgreen:

Is it possible for a PHP script running on a Linux server to receive a package (binary string) of data of random size and store it to disk? Can anybody post a pseudo code (mock-up) PHP program for this so I can use it as starting point?

Thanks a lot.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: I need to send binary data from a Windows app. to a web site

Post by Eran »

The php part would be pretty easy - depending on the method you choose to transport your data, most commonly using GET or POST headers:

Code: Select all

 
$filename = '/path/to/file.ext'; 
$data = $_POST['data'];
file_put_contents($filename,$data);
 
$_POST is an array of parameters sent with POST headers. Similarly $_GET would contain data sent with GET headers (simply as a parameter string through the address line). Those two arrays are superglobal variables which are available always for a PHP script environment.

For binary data you most likely be using POST, or a specific input stream wrapper - see this link for a complete list of those.

The more technical aspect of this would be in your application site, constructing the request (PHP being a web language, has plenty of ready-made ways to do this). Depending on your language of choice this could be very easy or just moderately easy ;)
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: I need to send binary data from a Windows app. to a web site

Post by bugmenot »

Hi Pytrin.

I use Delphi and I can already send data (POST) to web server made (HTTP port 80) also in Delphi.
I "just" need to replace the Delphi web server with a PHP script. Pretty difficult when you know knowing about PHP :)

What I need is the PHP script that will receive the data (POST) and store it to disk.


I think I need something like this:

<form action="file.php" method="POST">
Your Name:
<input type="text" name="user_name_field" id="user_name_field">
<br />
<input type="submit" value="Enter My Name in DB">
</form>


...but without the SUBMIT button.




------------------
Delphi way :) of POST-ing:

procedure THttpPostForm.PostButtonClick(Sender: TObject);
var Data : String;
begin
Data := 'FirstName=' + UrlEncode(Trim(FirstNameEdit.Text)) + '&' + 'LastName=' + UrlEncode(Trim(LastNameEdit.Text)) + '&' + 'Submit=Submit';
HttpCli1.SendStream := TMemoryStream.Create;
HttpCli1.SendStream.Write(Data[1], Length(Data));
HttpCli1.SendStream.Seek(0, 0);
HttpCli1.RcvdStream := TMemoryStream.Create;
HttpCli1.URL := Trim(ActionURLEdit.Text);
HttpCli1.PostAsync;
end;
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: I need to send binary data from a Windows app. to a web site

Post by Eran »

I think you misunderstood the flow a little - what you described is a form (or controls, whatever you want to call it). I already presented you with the script that can handle a POST request and save it into a file (yep, 3 lines - it's that simple). Of course it's completely barebones, with no checks or validation, but you should be able to take it from there.

For a more general knowledgebase you should get acquainted with the PHP manual, its an amazing resource - http://www.php.net

If you have any more specific questions, we'll be happy to help.
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: I need to send binary data from a Windows app. to a web site

Post by bugmenot »

pytrin wrote:I think you misunderstood the flow a little - what you described is a form (or controls, whatever you want to call it). I already presented you with the script that can handle a POST request and save it into a file (yep, 3 lines - it's that simple).
If you have any more specific questions, we'll be happy to help.
Somebody pointed me to http://us.php.net/manual/en/features.file-upload.php and I think I understand now your message now. I think I got the right direction now.
Thanks a lot.

By the way, among all web oriented programming languages, PHP rules!
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: I need to send binary data from a Windows app. to a web site

Post by bugmenot »

Ok. I started to build my first PHP program.
I don't think it will have more than 30 lines.
:mrgreen: :mrgreen: :mrgreen:
bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: I need to send binary data from a Windows app. to a web site

Post by bugmenot »

I am advancing.
However, I need to debug the POST package I send from my windows app. I think it is not properly formated because I get this:

[16-Nov-2008 17:22:23] PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

Where/how can I see the HTTP POST request that my app sends?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: I need to send binary data from a Windows app. to a web site

Post by Eran »

bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: I need to send binary data from a Windows app. to a web site

Post by bugmenot »

pytrin wrote:This might be useful - http://www.php.net/manual/en/reserved.v ... stdata.php
The manual says:
However, the preferred method for accessing the raw POST data is php://input. $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data".
While my HTML form is:

Code: Select all

<form enctype="[color=#FF0000]multipart/form-data[/color]" action="http://www.DNABaser.com/1/index.php" method="POST">
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: I need to send binary data from a Windows app. to a web site

Post by Eran »

bugmenot
Forum Newbie
Posts: 23
Joined: Fri Oct 10, 2008 11:44 am

Re: I need to send binary data from a Windows app. to a web site

Post by bugmenot »

Problem solved. Thank you very much for help.
Post Reply