writing http post contents to file in php - php

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:-
<?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 how to solve this.

if you know the input field name then use this.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
echo "Hello\n";
$somecontent = array();
$somecontent[] = $_POST['firstname'];
$somecontent[] = $_POST['lastname'];
//etc...
$my_file = "resp.txt";
$handle = fopen($my_file, "wb") or die('Cannot open file: '.$my_file);
fwrite($handle, $somecontent);
fclose($handle);
}
else {
echo "Error\n";
}
?>

does this work?
if(!empty($_POST){
file_put_contents('resp.txt', serialize($_POST));
}

Related

Why can't I see my .txt file from this php script

I'm using this script to see the content of a .text file -
<?php
$file = fopen("Gin_List_Website.txt","r");
if(!file)
{
echo("ERROR:cant open file .. :(");
}
else
{
echo("opened the file .. :)");
$buff = fread ($file,filesize("Gin_List_Website.txt"));
print $buff;
echo $buff;
}
?>
But all I see is this line
opened the file .. :)
What am I doing wrong?
First you need to change if(!file) with if(!$file).
Here is more simple code example:
<?php
$file = fopen("Gin_list_Website.txt", "r") or die("Unable to open file!");
echo fread($file,filesize("Gin_list_Website.txt"));
fclose($file);
?>

PHP data processing not outputting

Hi I am trying to get this external text file to print inside my php document. The code looks fine to me however when I echo it does not output anything and I am not sure why this is. Can anybody help me out as I am new to this.
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
$fp = fopen($location, 'r');
if ($fp) {
$readin = fread($fp);
fclose($fp);
} else {
echo 'Can\'t open input.txt';
}
Not sure what you're trying to 'echo' but have you checked if the file exists in the first place?
Your code could be written as:
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
if (file_exists($location) && $data = file_get_content($location)){
echo $data;
} else {
echo 'File not found';
}
if (file_exists($location) && $file = fopen($location, 'r')){
$file_content = fread($file, filesize($location));
fclose($file);
} esle {
echo 'File not found';
}
See here for more: http://php.net/manual/en/function.file-get-contents.php, http://php.net/manual/en/function.filesize.php

generate html file and download it with php

First goal: Create an index.html file and create a link to download the generated file
The issue here is when i click to generate new file the downloaded file is always the same and isnt updated
the variable $content has insite an entire html page with <headers><aside> and <sections>
I have the following code
if( empty( $error )){
echo "<h3>File generated</h3>";
$my_file = 'index.html';
if (file_exists($my_file)) {
if(unlink($my_file)){
};
$new_file = 'index.html';
$handle = fopen($new_file, 'w') or die('Cannot open file: '.$new_file);
$data = $content;
fwrite($handle, $data);
fclose($handle);
echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>";
} else {
$new_file = 'index.html';
$handle = fopen($new_file, 'w') or die('Cannot open file: '.$new_file);
$data = $content;
fwrite($handle, $data);
fclose($handle);
echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>";
}
hi i think it's will be work if variable $content will be contain correct data and you have permission to read, update, create file. in code I use file_put_contents function for minimize your code
if( empty( $error )){
echo "<h3>File generated</h3>";
$my_file = 'index.html';
file_put_contents($my_file, $content);
echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>";
}
UPDATE:
Are you sure that you have correctly generated content and the $content always contains different value?

Create file.txt in php after ajax function always returns false

I want to create new .txt files but as this code is always returning false, the ajax success function is not executed.
the all code is:
<?php
$nome = $_POST["nome"];
$datanasc = $_POST["datanasc"];
$genero = $_POST["genero"];
$nat = $_POST["nat"];
$morada = $_POST["morada"];
$mail = $_POST["mail"];
$existe = false;
$myFile = "Users.txt";
$myFile1 = "Current_User.txt";
$fh = fopen($myFile, "r")or die("can't open file");
while (($line_of_text = fgets($fh))) {
$Data = explode(';', $line_of_text);
if($nome == $Data[0] && $datanasc == $Data[1] && $genero == $Data[2] && $nat == $Data[3] && $morada == $Data[4] && $mail == $Data[5]){
$existe = true;
break;
}
}
fclose($fh);
if($existe == true){
$arrayToJs["existe"] = $existe;
}
else{
$arrayToJs["existe"] = $existe;
$fh = fopen($myFile, "a")or die("can't open file");
$stringData = $nome.";".$datanasc.";".$genero.";".$nat.";".$morada.";".$mail.";"."\n";
//print_r($stringData);
fwrite($fh, $stringData);
fclose($fh);
$fh1 = fopen($myFile1, "w")or die("can't open file");
fwrite($fh1, $stringData);
fclose($fh1);
there is the problem in the code cause is returning false and the ajax success function is not executed. . .
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
$ffav = $nome.'_Favoritos.txt';
$handle = fopen($ffav, 'w') or die('Cannot open file: ');
fclose($ffav);
$fcart = $nome.'_Cesto.txt';
$handle = fopen($fcart, 'w') or die('Cannot open file: ');
fclose($fcart);
}
}
echo json_encode($arrayToJs);
?>
Thank you all guys!
Use the file pointer ($handle) you created with fclose:
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
$ffav = $nome.'_Favoritos.txt';
$handle = fopen($ffav, 'w') or die('Cannot open file: ');
fclose($handle);
$fcart = $nome.'_Cesto.txt';
$handle = fopen($fcart, 'w') or die('Cannot open file: ');
fclose($handle);
}
Otherwise your file will always return PHP error when those files do not exist
Whether or not your AJAX success function gets called has nothing to do with a PHP code's "return value."
Assuming you're using jQuery or one of the other JavaScript frameworks, it has to do with the HTTP response code. Presumably, you're probably encountering a PHP error which is resulting in a 500 response back to the browser. This would end you up in the error handler instead of the success handler.
Have you tried using something like the network inspector in Chrome (or the Net tab in Firebug) to investigate the actual HTTP response?
This code is not returning any value please pass value (true/false) after file created to ajax response.
If you pass a relative path to file_exists, it will return false unless the path happens to be relative to the current PHP directory.

How to Search and Find in Txt file. Then Using The Found Variable

OK, this is Another Project Im Working ON.
Its a Chat Client. and Using it For Staff
I want the server to have a staff.txt on it.
and I want the php file to do this.
Execute the php.
if The Submitted Username is Found in the staff.txt then
The Username changes to [Staff]"Username Here"
I got the search and find down.
I Cant seem to keep the username that was submitted, and just adding staff to it.
Im Adding my Source Now.
<?php
// Parameters (Leave this Alone)
$Message = $_GET["message"];
$Username = htmlspecialchars($_GET["username"]);
$time = ($_GET["time"]);
// User Banning
$data = file_get_contents('Banned.txt');
if(strpos($data, $Username) !== FALSE)
{
die();
}
else
{
// File Writing (Leave this Alone)
$File = "Chat.txt";
$Handle = fopen($File, "a");
fwrite($Handle, $Username);
fwrite($Handle, ": ");
fwrite($Handle, $Message);
fwrite($Handle, " -:-:- ");
fwrite($Handle, $time);
fwrite($Handle, "\r\n");
print "Message Sent";
fclose($Handle);
}
?>
I have user banning working, and i Want the Staff To Work in the same way.
Any Help would be appreciated
Trying it a different way
If ($Username=="!divider!StaffMember1") $Username="!divider![Staff] StaffMember1";
If ($Username=="!divider!StaffMember2") $Username="!divider![Staff] StaffMember2";
that seems to work fine in the php file thats running the php with everything else.
Is there a way to have that list in a seperate file? .txt file or .php doesnt matter.
You can just do it like the banlist:
<?php
// Parameters (Leave this Alone)
$Message = $_GET["message"];
$Username = htmlspecialchars($_GET["username"]);
$time = ($_GET["time"]);
// check staff
$data = file_get_contents('staff.txt');
if(strpos($data, $Username) !== FALSE)
$Username = '[STAFF]' . $Username;
// User Banning
$data = file_get_contents('Banned.txt');
if(strpos($data, $Username) !== FALSE)
{
die();
}
else
{
// File Writing (Leave this Alone)
$File = "Chat.txt";
$Handle = fopen($File, "a");
fwrite($Handle, $Username);
fwrite($Handle, ": ");
fwrite($Handle, $Message);
fwrite($Handle, " -:-:- ");
fwrite($Handle, $time);
fwrite($Handle, "\r\n");
print "Message Sent";
fclose($Handle);
}
?>

Categories