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.
Related
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?
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 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 9 years ago.
Is it possible to check if the string that is being added to file is not already in the file and only then add it? Right now I am using
$myFile = "myFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $var . "\n";
fwrite($fh, $stringData);
fclose($fh);
But I get many duplicates of $var values and wanted to get rid of them.
Thank you
use this
$file = file_get_contents("myFile.txt");
if(strpos($file, $var) === false) {
echo "String not found!";
$myFile = "myFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $var . "\n";
fwrite($fh, $stringData);
fclose($fh);
}
$myFile = "myFile.txt";
$filecontent = file_get_contents($myFile);
if(strpos($filecontent, $var) === false){
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $var . "\n";
fwrite($fh, $stringData);
fclose($fh);
}else{
//string found
}
Best way is use file_get_contents & perform operation only if $var is not in your file.
$myFile = "myFile.txt";
$file = file_get_contents($myFile);
if(strpos($file, $var) === FALSE)
{
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $var . "\n";
fwrite($fh, $stringData);
fclose($fh);
}
Possible solution could be :
1. Fetch the contents using fread or file_get_contents
2. Compare the contents with the current contents in file
3. add it if it is not there.
function find_value($input) {
$handle = #fopen("list.txt", "r");
if ($handle) {
while (!feof($handle)) {
$entry_array = explode(":",fgets($handle));
if ($entry_array[0] == $input) {
return $entry_array[1];
}
}
fclose($handle);
}
return NULL;
}
You can do the it like this also
$content = file_get_contents("titel.txt");
$newvalue = "word-searching";
//Then use strpos to find the text exist or not
you could save all your added string in an array and the check with in_array if the current string has been added or not.
Second choice is to read the file each time you want to write and do a strstr on it.
I believe fgets is the answer here.
$handle = fopen($path, 'r+'); // open the file for r/w
while (!feof($handle)) { // while not end
$value = trim(fgets($handle)); // get the trimmed line
if ($value == $input) { // is it the value?
return; // if so, bail out
} //
} // otherwise continue
fwrite($handle, $input); // hasn't bailed, good to write
fclose($handle); // close the file
This answer is based solely on the fact that you have appended a newline ("\n") in your code, which is why fgets will work here. This may be preferable over pulling the whole file into memory with file_get_contents(), simply because the size of the file may be prohibitive of that.
Alternatively, if the values are not newline delimited, but are fixed length, you can always use the $length argument of fgets() to pull exactly $n characters out (or use fread() to pull exactly $n bytes out)
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!
we unable to split the string following code.please Help us.
<?php
$i=0;
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "no\t";
fwrite($fh, $stringData);
$stringData = "username \t";
fwrite($fh, $stringData);
$stringData ="password \t";
fwrite ($fh,$stringData);
$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = "1\t";
fwrite($fh, $stringData1);
$stringData1 = "srinivas \t";
fwrite($fh, $stringData1);
$stringData1 ="malayappa \t";
fwrite ($fh,$stringData1);
fclose($fh);
?>
$fh = fopen("testFile.txt", "r");
$
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
}
fclose($fh);
$Beatles = array('pmm','malayappa','sreenivas','PHP');
for($i=0;$i<count($Beatles);$i++)
{
if($i==2)
{
echo $Beatles[$i-1];
echo $Beatles[$i-2];
}
}
$pass_ar=array();
$fh = fopen("testFile.txt", "r");
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
$t1=explode(" ",$line);
print_r($t1);
array_push($pass_ar,t1);
}
fclose($fh);
If i read the code correctly you are writing the string seperated by \t but try to explode with spaces, use:
explode("\t", $string);
You could use fgetcsv, since you're just doing a standard tab-delimited input file. Given your sample file of:
no [tab] username [tab] password
1 [tab] srinivas [tab] malayappa
then
$lines = array();
$fh = fopen('testfile.txt', 'rb') or die ("can't open testfile.txt");
while($lines[] = fgetcsv($fh, 0, "\t") { // no line length limit, tab delimiter)
...
}
will give you
$lines = Array(
0 => Array(
0 => 'no ',
1 => 'username ',
2 => 'password '
),
1 => Array(
0 => 1,
1 => 'srinivas ',
2 => 'malayappa'
)
);
You're exploding on whitespace. Unless there is whitespace in the string your exploding then no, it won't work.
Try using the code markup to make your code a little more readable in order to get better quality responses from people.