How break a line of code [duplicate] - php

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.

Related

why doesnt my mysql_fetch_array or mysql_fetch_assoc does not work

i want to get the result of mysql query into a text, currently it works when i type mysql_num_fields and mysql_num_rows, but it does not work mysql_fetch_array or mysql_fetch_assoc which is the genuine requirement.. can anybody kindly tell me what wrong with this code
$query = "CALL create_report($ID, '$startDate', '$endDate', $endlmt, $position);";
$Localcon = $this->getConnection();
$result = mysql_query($query, $Localcon);
//$debug = mysql_num_rows($result);
$myFile = "debug.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "-------------\n";
fwrite($fh, $stringData);
$stringData = mysql_fetch_array($result);
fwrite($fh, $stringData);
fclose($fh);
edited code
$stringData_2 = mysql_fetch_array($result);
foreach ($stringData_2 as $string) {
$stringData = "----------------\n";
fwrite($fh, $stringData);
fwrite($fh, $string);
}
The problem is mysql_fetch_array returns an array, while fwrite is expecting a string. Here is the error your code produces:
fwrite() expects parameter 2 to be string, array given in ...
The 2 param of fwrite, $stringData, needs to be a string.
Depending on what you are trying to do, you might start by trying something like this, then play around with other alternatives:
...
foreach($result as $str) {
fwrite($fh, $str);
}
...
Sidenote: Why shouldn't I use mysql_* functions in PHP?

To write to another text file by appending the data in codeigniter [duplicate]

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.

PHP: Not able to add all values to the string [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to add unknown number of parameters to a string in PHP.
Here is code:
<?php
print "Get parameters";
$myFile = "wr.txt";
$fh = fopen ($myFile, 'w') or die ("can't open file");
$stringData = '';
//$st = '';
foreach ($_REQUEST as $k => $v) {
$date = date ('Y-m-d H:i:s');
$stringData = $k.":".$v."Date:".$date."\n";
fwrite ($fh, $stringData);
fwrite ($fh, $stringData);
fclose ($fh);
}
?>
But it is adding only last parameter value.
I also tried like
$stringData += $k.":".$v."Date:".$date."\n";
And put this:
fwrite($fh, $stringData);
fwrite($fh, $stringData);
fclose($fh);
Outside to the for loop but in that case it is writing 00 in the wr.txt. Please help me how can i write all the paramets in a line by date.
Thanks
In PHP . is used for concatenation + is not used for concatenation.
change this
$stringData +=$k.":".$v."Date:".$date."\n";
to
$stringData .= $k.":".$v."Date:".$date."\n";
Move close ($fh) away from the loop:
$myFile = "wr.txt";
$fh = fopen ($myFile, 'w') or die ("Can't open file");
$date = date ('Y-m-d H:i:s');
foreach ($_REQUEST as $k => $v)
fwrite ($fh, "$k: $v Date $date\n") or die ("Cannot write to file");
fclose ($fh);
use "." instead of "+", in php concatenation operator is "."
SEE THE LINES :
$stringData .=$k.":".$v."Date:".$date."\n";//** ADD CONCATENATION '.' BEFORE '='**
fwrite($fh, $stringData); //PUT THIS OUT SIDE THE LOOP
fwrite($fh, $stringData); //**REMOVE THIS **
fclose($fh); // **PUT HIS OUT SIDE THE FOREACH LOOP**
Try this :
<?php
print "Get parameters";
$myFile = "wr.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = '';
//$st = '';
foreach($_REQUEST as $k => $v) {
$date = date('Y-m-d H:i:s');
$stringData .=$k.":".$v."Date:".$date."\n";//** ADD CONCATENATION '.' BEFORE '='**
//fwrite($fh, $stringData); PUT THIS OUT SIDE THE LOOP
///fwrite($fh, $stringData); //**REMOVE THIS **
//fclose($fh); // **PUT HIS OUT SIDE THE FOREACH LOOP**
}
fwrite($fh, $stringData);
fclose($fh);
?>
I can only guess what you are trying to do. but try this:
<?php
print "Get parameters";
$myFile = "wr.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = '';
//$st = '';
foreach($_REQUEST as $k => $v) {
$date = date('Y-m-d H:i:s');
$stringData = $stringData . $k.":".$v."Date:".$date."\n";
}
fwrite($fh, $stringData);
fclose($fh);
?>
Hope that helps.

Receiving post data xml

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.

Writing to Text files via php

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!

Categories