Delphi and PHP. I want to send a POST from Delphi to a PHP script

Posted on 2008-11-17
Medium Priority
Last Modified: 2013-12-13
I want my Delphi 7 app to send a very small package of data (under 200 bytes) to a PHP script.
The script will store the data to the local disk in a file.

I use ICS 6 component to send the POST request.

I have PHP the script which is working when it receive the POST from a HTML form, but it is not working when it receives that POST from my app.

So, what is wrong with the POST sent by my app?

Delphi code (from ICS examples folder):

procedure THttpPostForm.PostButtonClick(Sender: TObject);
VAR Data : String;
    Data :=       'MAX_FILE_SIZE=30000&';
    Data := Data+ 'nume_fis=' + {UrlEncode}(Trim(sFileName))+ '&';
    Data := Data+ 'Submit=Submit';

    HttpCli.SendStream := TMemoryStream.Create;
    HttpCli.SendStream.Write(Data[1], Length(Data));
    HttpCli.SendStream.Seek(0, 0);
    HttpCli.RcvdStream := TMemoryStream.Create;
    HttpCli.URL := Trim(ActionURLEdit.Text);

PHP code:



    $str = '';
    foreach($_POST as $P=>$p){
        $str .= "$P => $p\r\n";
    $str .= "====================\r\n";
    $f = fopen('post.txt', 'a+');
    fwrite($f, $str);
    print('POST request saved to disk as post.txt<br>');      
else { print('No POST received!'); }

print('Debugging 2:<br>');
print('Size: '.$_FILES['nume_fis']['size'].'<br>');
print('Name: '.$_FILES['nume_fis']['name'].'<br>');
print('Eror: '.$_FILES['nume_fis']['error'].'<br>');
print('Type: '.$_FILES['nume_fis']['type'].'<br>');
print('Tmp_name: '.$_FILES['nume_fis']['tmp_name'].'<br>');

$uploaddir = '';
$uploadfile = $uploaddir . basename($_FILES['nume_fis']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['nume_fis']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n"; }

echo 'Here is some more debugging info:';

print "</pre>";

HTML form to upload data:
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="index.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->    
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: 
    <input name="nume_fis" type="file" size="40" />
    <input type="submit" value="Send File" />

Open in new window

Question by:fedra
  • 3

Author Comment

ID: 22974756
Of course you don't have to write the PHP code that stores the info to disk (this will be another post in PHP section) but only the Delphi code :)

Accepted Solution

fedra earned 0 total points
ID: 22976368
Points increased.

Link to ICS component: http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html 

Author Comment

ID: 22989637
I will reopen the question but in a different way.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question