writing http post contents to file in php

Links for php scripts

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Sat Apr 13, 2013 1:51 am

writing http post contents to file in php

Postby sattu » Sat Apr 13, 2013 1:55 am

Hi Everyone,
I am posting a string xml data to a php page hosted in IIS. All I want is to be able to read the string data that I receive in php page and write it to a file. But am not able to achieve the same. Here is my code:-

Code: Select all

     echo "Hello\n";
     $somecontent = print_r($_POST, TRUE);

     $my_file = "resp.txt";
     $handle = fopen($my_file, "w") or die('Cannot open file:  '.$my_file);
     fwrite($handle, $somecontent); 
}else {
     echo "Error\n";

But am not able to create the file or read the POST contents. I would be glad if someone would figure out what I am doing wrong.

php-forum Fan User
php-forum Fan User
Posts: 858
Joined: Mon Oct 01, 2012 12:32 pm

Re: writing http post contents to file in php

Postby seandisanti » Mon Apr 15, 2013 7:46 pm

try this....

Code: Select all

if ($_POST){ //handling post data
echo 'Post received, writing data to file:<br />';
$fh fopen('test.txt','w');
    if (!
$fh){ //file open failed
trigger_error('Failed to open file for writing');
    foreach (
$_POST as $key=>$value){
$ack fwrite($fh,'Value for ' $key ' is: ' $value);
        if (!empty(
'Value written for ' $key '<br />';
} else { 
//not responding to a form, so show one...
echo '<form name="whatever" action="' $_SERVER['PHP_SELF'] . '" method="post">';
'<input type="text" name="input1" placeholder="Enter text here" /><br />';
'<input type="text" name="input2" placeholder="Enter text here" /><br />';
'<input type="text" name="input3" placeholder="Enter text here" /><br />';
'<input type="Submit" value="try to write to file" />'"\n";

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest