I have a little issue while trying to kill time. I am writint to a text file the contents of the filled in form so it is easy to read. At the moment everything gets put int a single line, and therfore I can not tell where msgs begin or end. I wrote a php like this:
$from = $_POST[from];
$friend = $_POST[friend];
$carrier = $_POST[carrier];
$message = stripslashes($_POST[message]);
if ((empty($from)) || (empty($friend)) || (empty($message))) {
header ("Location: sms_error.php");
}
else if ($carrier == "orange_mobile_network") {
$formatted_number = $friend."#orange_mobile_network.co.uk";
mail("$formatted_number", "SMS", "$message");
header ("Location: sms_success.php");
}
Then the SMS/Text message will be sent. After this I wanted to write/store/append the message on a txt file. So I wrote:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $_POST[from];
fwrite($fh, $stringData);
$stringData = $_POST[friend];
fwrite($fh, $stringData);
$stringData = $_POST[message];
fwrite($fh, $stringData);
fclose($fh);
But like I said. Everything works. I just want to be able to read the file and put everything in a new line and possibly format it nicely for easy reading. I do not want to use a DB for storing the TXT as my host is going to charge me.
change this:
fwrite($fh, $stringData);
with this:
fwrite($fh, $stringData . PHP_EOL);
or:
fwrite($fh, $stringData . "\r\n");
You can append PHP_EOL when you write for a platform aware newline. I.e. $stringData = <string> . PHP_EOL;
If don't need it to be platform aware, going with the windows newline is a safe option.
define('CRLF', "\r\n");
$stringData = <string> . CRLF;
fwrite($fh, $stringData);
I do not know exactly Cause you're using. txt instead of a bank, but my tip for this specific case is to work with json format.
Example:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
//assembles a string of type json
$data = json_encode(array(
'from' => $_POST[from],
'friend' => $_POST[friend],
'message' => $_POST[message]
));
//Write in the file
fwrite($fh, $data);
fclose($fh);
//reads the file
$content = file_get_contents($myFile);
$object = json_decode($content);
var_dump($object);
good it is at least better organized.
a hug!
Related
This question already has answers here:
Writing a new line to file in PHP (line feed)
(4 answers)
Closed 9 years ago.
I am trying to break the line after this code. I can't figure it out what ever I do the output of my code is just as shown below:
Output:
User: adminLogged in: 2014-02-09 05:34:30User: adminLogged OUT:
2014-02-09 05:34:36User: tataLogged in: 2014-02-09 05:34:41User:
tataLogged OUT: 2014-02-09 05:34:43
I want to set some space and new line.
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData");
$stringData = "Logged in: $date";
fwrite($fh, "$stringData");
fclose($fh);
Just make use of \n or \r in front of your fwrite().
Like this.
fwrite($fh, "\n$stringData");
^-------- // Add this to both of your fwrite() calls
Try this
$date = date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData\n");
$stringData = "Logged in: $date";
fwrite($fh, "$stringData\n");
fclose($fh);
Here you just add \n before or after variable assign just like this fwrite($fh, "$stringData\n"); or fwrite($fh, "\n$stringData");.
For more details, see this question.
This question already has answers here:
PHP fwrite new line
(4 answers)
Closed 9 years ago.
$myFile = APPPATH.'/../log.txt';
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);
Above shown is my code.
output :
New Stuff 1New Stuff 2
I need New Stuff 2 in the second line
Also the newly written data should append to the existing data.How to fix the issue.
You need to use PHP Predefined Constant PHP_EOL
$stringData = "New Stuff 1" . PHP_EOL;
fwrite($fh, $stringData);
$stringData = "New Stuff 2" . PHP_EOL;
fwrite($fh, $stringData);
it will produce \r\n
file_put_content('your.txt',"data\n". PHP_EOL,FILE_APPEND);
try this.by the way if you open file in browser will will not show you next line with "\n" try to see it using view page source.or use notepad++ to se it.
I am currently building a form that sends a user off to an external payment site at a certain point during the form.
At this point I have provided two URLs to the service that is providing this payment service for me, one to call the function that will update a users record at the point of pyment, and a URL that a user is directed back to after the payment process is complete.
The first URL (function call to update a users record) is "out-of-band" and will never be displayed to the user and will be from the server rather than client IP. Because of this I am in a bit of a 'blind-alley' as I cannot see the variables that are being passed back to me, and obviously these are important in order for me to parse and manipulate this data (post) in order to update our records.
I have tried writing a simple algorithm inside my function that would loop through these POST variables and write them to a text file but unfortunately I couldn't get this working (maybe something to do with Joomla?).
Here is that function:
public function harlandReturn() {
$myFile = "text.txt";
$fh = fopen($myFile, 'w');
$stringData = "";
foreach($_POST as $i => $v) {
$stringData .= $i . " : " . $v . " / ";
}
fwrite($fh, $stringData);
fclose($fh);
}
My main question is, is there a way i can get a function to log and store any data sent to it in a file, email or something similar so I can see what they are (ints, strings, boolean, etc) and their names?
Thanks
Because variables aren't always predictable - for instance, subarrays - you're probably best serializing the array and writing that to the file.
if(is_writable($myFile)) {
$fh = fopen($myFile, 'w');
$stringData = serialize($_POST);
fwrite($fh, $stringData);
fclose($fh);
}
You can then either open the file directly or create a quick script using unserialize to view it as POSTed.
Note that you're not doing any error checking to see if the file is writable, etc.
your function is missing fwrite()
$myFile = "text.txt";
$fh = fopen($myFile, 'w');
foreach($_POST as $i => $v) {
$stringData = $i . " : " . $v . " / ";
fwrite($fh, $stringData);
}
fclose($fh);
or
$myFile = "text.txt";
$fh = fopen($myFile, 'w');
foreach($_POST as $i => $v) {
$stringData .= $i . " : " . $v . " / ";
}
fwrite($fh, $stringData);
fclose($fh);
Hi I need to receive a post data which will be in an xml that is encoded in a base64 format.
I'll be receiving this from a payment gateway. Now all i get is this. My code creates a txt file but is empty. Is there anything wrong with the code? The output should be an xml envelope in a text file.
$body = '';
$fh = #fopen('php://input', 'r');
if ($fh)
{
while (!feof($fh))
{
$s = fread($fh, 1024);
echo $s;
if (is_string($s))
{
$body .= $s;
}
}
fclose($fh);
}
$body = base64_decode($body);
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
I tried to contact the payment gateway and they are telling me that they are getting this error "The remote server returned an error: (417) Expectation failed." where could the problem exist us or them?
Since your file is returning blank, I would recommend verifying the specifications from the payment gateway for your fopen() function. In addition, if you are properly getting data back from them, then I would check the base64_decode() function. I have seen situations where there may be a header or other data at the top of the actual payload data that fouls up the base64_decode and ruins your day.
It looks like you are mixing up your file handlers, can you see if the following codes run:
$body = $HTTP_RAW_POST_DATA;
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$stringData = $body;
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
I have just tested the above code myself successfully.
What I suggest you do is trying the following:
Try to put a static piece of data into $body (eg: "$body = 'test';")- and see if that saves - if not then it is an issue at your end.
Delete the file (to remove any permission issues).
Double check the url that the payment gateway is sending to is correct.
I want to fetch json script and write it to a txt file undecoded, exactly how it was originally. I do have a script that I use that I am modifying but unsure what to commands to use. This script decodes, which is what I want to advoid.
//Get Age
list($bstat,$bage,$bdata) = explode("\t",check_file('./advise/roadsnow.txt',60*2+15));
//Test Age
if ( $bage > $CacheMaxAge ) {
//echo "The if statement evaluated to true so get new file and reset $bage";
$bage="0";
$file = file_get_contents('http://somesite.jsontxt');
$out = (json_decode($file));
$report = wordwrap($out->mainText, 100, "\n");
//$valid = $out->validTo;
//write the data to a text file called roadsnow.txt
$myFile = "./advise/roadsnow.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $report;
fwrite($fh, $stringData);
}
else {
//echo the test evaluated to false; file is not stale so read local cache
//print "we are at the read local cache";
$stringData = file_get_contents("./advise/roadsnow.txt");
}
// if/else is done carry on with processing
//Format file
$data = $stringData
Try this:
// Get JSON Data
$json_data = file_get_contents('http://somesite.jsontxt');
// Write JSON to File
file_put_contents('json_data.txt', $json_data);