So.. i have problems with this code :
$filename = "info.txt";
$tex = $_POST['Name'];
$text = $tex . $_POST['Surname'];
$fp = fopen ($filename, "w");
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
It saves ONLY 1 name & surname. after another person submits his info, previous information is lost.
Is it possible to save all information?
You have to change the mode for fopen :
change 'w' to 'a' (a for append)
You'll find more information about each mode right here:
http://us2.php.net/manual/en/function.fopen.php#refsect1-function.fopen-parameters
Related
When I request this URL:
http://example.com/csv_build.php?filename=test.txt&time=1485902100000&data=25
I expect the PHP code to create a file named : datafile.txt that will contain the following data:
test.txt,1485902100000,25<CR><LF>
For some reason, datafile.txt is not created.
Did I made a mistake ?
<?
# GRAB THE VARIABLES FROM THE URL
$File = 'datafile.txt';
$FileName = $_GET['filename'];
$HeureTronconMesure = $_GET['time'];
$DonneeCapteur = $_GET['data'];
# --------------------------------
$FICHIER = fopen($File, "a");
#ftruncate($FICHIER,0);
fputs($FICHIER, $FileName);
fputs($FICHIER , ",");
fputs($FICHIER, $HeureTronconMesure);
fputs($FICHIER , ",");
fputs($FICHIER, $DonneeCapteur);
fputs ($FICHIER , "\r\n");
fclose($FICHIER);
?>
You're not doing any error checking. Why do you assume everything will work okay? If you spot some errors, first thing I would check is file permissions.
<?php
# GRAB THE VARIABLES FROM THE URL
$File = str_replace("/", "_", $_GET["filename"]);
$FileName = $_GET['filename'];
$HeureTronconMesure = $_GET['time'];
$DonneeCapteur = $_GET['data'];
# --------------------------------
if (!$FICHIER = fopen($File, "a")) {
//there was an error opening the file, do something here
}
fputs($FICHIER, $FileName);
fputs($FICHIER , ",");
fputs($FICHIER, $HeureTronconMesure);
fputs($FICHIER , ",");
fputs($FICHIER, $DonneeCapteur);
fputs ($FICHIER , "\r\n");
fclose($FICHIER);
?>
Though really, I'd simplify this code immensely by using some shortcuts and concatenating strings.
<?php
// replace any slashes in the filename with underscores
$file = str_replace(["/", "\\"], "_", $_GET["filename"]);
// build a string out of your data
$data = "$_GET[filename],$_GET[time],$_GET[data]\r\n";
// write the data to the file, checking if it returns false
if (!file_put_contents($file, $data)) {
//there was an error writing the file, do something here
}
Note, never open your code with <?, it's been deprecated a very long time now. Use <?php.
I am trying to make a program where I can add things to a list, read things, and clear the list. I have the clear function working perfectly, however I can't seem to add or read more than 1 line at a time. I am using fwrite($handle, $MyString); but that replaces everything in the entire file with $MyString. To get the information from the file I am using $list = fgets($handle); and then using echo to print it. This reads the first line in the file only.
Any help?
Thanks!
Getlist code:
<?php
$myFile = "needlist.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>
Add to the list code:
<?php
$neededlist = "needlist.txt";
$fh = fopen($neededlist, 'w');
$user_message = $_REQUEST['txtweb-message'];
$needed .= $user_message;
$needed .= "\n";
fwrite($fh, $needed);
fclose($fh);
echo "You have successfully added ", $user_message;
?>
When you write to the file are you opening your filehandle with the "a" mode option? Opening with "w" or "x" truncates it so you start with a clean file (http://php.net/fopen)
fgets(); reads only until the end of the line ( http://php.net/fgets ). To get the whole file you can try:
var $list = "";
var $line = "";
while ($line = fgets($handle)) {
$list = $list . "\n" . $line;
}
echo $list;
You want to add the "\n" because fread doesn't read the linefeeds IIRC. There're also a couple functions that might be more appropriate in this situation like file_get_contents and fread.
Fgets returns only one string. You should use it in cycle like that:
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
I have an issue with writing and reading to text file.
I have to first write from a text file to another text file some values which I need to read again. Below are the code snippets:
Write to text file:
$fp = #fopen ("text1.txt", "r");
$fh = #fopen("text2.txt", 'a+');
if ($fp) {
//for each line in file
while(!feof($fp)) {
//push lines into array
$thisline = fgets($fp);
$thisline1 = trim($thisline);
$stringData = $thisline1. "\r\n";
fwrite($fh, $stringData);
fwrite($fh, "test");
}
}
fclose($fp);
fclose($fh);
Read from the written textfile
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
echo rtrim($kw[$i]);
}
But, if I am not mistaken due to the "/r/n" I used to insert the newline, when I am reading back, there are issues and I need to pass the read values from only the even lines to a function to perform other operations.
How do I resolve this issue? Basically, I need to write certain values to a textfile and then read only the values from the even lines.
I'm not sure whether you have issues with the even line numbers or with reading the file back in.
Here is the solution for the even line numbers.
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
$myValue = rtrim($kw[$i]);
if(i % 2 == 0)
{
echo $myValue;
}
}
I'm trying to open an encrypted file that will store a list of information, then add a new ID with information, and save the file back as it was originally encrypted. I have xor/base64 functions that are working, but I am having trouble getting the file to retain old information.
here is what I am currently using:
$key = 'some key here';
$id = $_GET['id'];
$group = $_GET['group'];
$file = "groups.log";
$fp = fopen($file, "w+");
$fs = file_get_contents($file);
$filedec = xorstr(base64_decode($fs),$key);
$info = "$id: $group";
$filedec = $filedec . "$info\n";
$reencode = base64_encode(xorstr($filedec,$key));
fwrite($fp, $reencode);
fclose($fp);
function xorstr($str, $key) {
$outText = '';
for($i=0;$i<strlen($str);)
{
for($j=0;$j<strlen($key);$j++,$i++)
{
$outText .= $str[$i] ^ $key[$j];
}
}
return $outText;
}
?>
It should save an entire list of the ID's and their corresponding groups, but for some reason it's only showing the last input :(
I wouldn't call this encryption. "cereal box decoder ring", maybe. If you want encryption, then use the mcrypt functions. At best this is obfuscation.
The problem is that you're doing fopen() before doing file_get_contents. Using mode w+ truncates the file to 0-bytes as part of the fopen() call. So by the time file_get_contents comes up, you've deleted the original file.
$fs = file_get_contents(...);
$fh = fopen(..., 'w+');
in that order will fix the problem.
Ok building a php webform need one of the entries to be the filename written.
How can I achieve this?
Here's my php code...
$filename = "output.txt"; #Must CHMOD to 666
$text = $_POST['bin'];
$text2 = $_POST['pcn'];
$text3 = $_POST['gnum'];
$text4 = $_POST['memid'];
$text5 = $_POST['urlen'];
$text6 = $_POST['urles'];
$text7 = $_POST['tlogo'];
# Form must use POST. if it uses GET, use the line below:
#$text = $_GET['theformfieldname']; #POST is the preferred method
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, "$text\r\n");
fwrite ($fp, "$text2\r\n");
fwrite ($fp, "$text3\r\n");
fwrite ($fp, "$text4\r\n");
fwrite ($fp, "$text5\r\n");
fwrite ($fp, "$text6\r\n");
fwrite ($fp, "$text7\r\n");
fclose ($fp);
header("Location: logo.html");
}
else {
echo ("There was an error please submit your request again");
}
?>
ok need $filename = "output.txt";
to be from the input $text3 = $_POST['gnum'];
something along the lines of:
$filename = $_POST['gnum'].txt;
but this wont work..
Thanks in advance,
Joe
instead of
$filename = $_POST['gnum'].txt;
you should use
$filename = $_POST['gnum'].".txt";
I'm not super clear on what you're asking, but I think you want to know how to appent '.txt' to the end of the $_GET variable?
If that's the case, you just need to do this:
$text3 = $_POST['gnum'] . '.txt';
Use this for getting the file name.
$_FILES["file"]["name"]