Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

writing http post contents to file in php

Links for php scripts

Moderators: macek, egami, gesf

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
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
     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.
sattu
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sat Apr 13, 2013 1:51 am

Re: writing http post contents to file in php

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

try this....
Code: Select all
<?php
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(
$ack)){
            echo 
'Value written for ' $key '<br />';
        }
        
    }
    
fclose($fh);
} else { 
//not responding to a form, so show one...
    
echo '<form name="whatever" action="' $_SERVER['PHP_SELF'] . '" method="post">';
    echo 
'<input type="text" name="input1" placeholder="Enter text here" /><br />';
    echo 
'<input type="text" name="input2" placeholder="Enter text here" /><br />';
    echo 
'<input type="text" name="input3" placeholder="Enter text here" /><br />';
    echo 
'<input type="Submit" value="try to write to file" />'"\n";
    
    echo 
'</form>';
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 773
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron