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

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

Who is Participating?
fedraConnect With a Mentor Author Commented:
Points increased.

Link to ICS component: 
fedraAuthor Commented:
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 :)
fedraAuthor Commented:
I will reopen the question but in a different way.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.