I've a text file.Now i want to replace text data with a form (POST or GET) in PHP.
My text file like that-
"
Mr.A
87362
Mr.B
87427
Mr.C
85423
"
Now if i want to change Mr.A to Mr.H with a form submit POST or GET method,what should i do?
Whenever yo have to change a text file, you always have to read it into memory, do string manipulation and write the whole thing to a file again.
This explains a lot Overwrite Line in File with PHP
Assuming you can get your variables from your POST request, you should be able to follow the solution in the above link to help solve your problem.
here is the code
<?php
$n="filename.txt";
$f=fopen($n,'r');
$c=fread($f,100);
$final = str_replace("Mr.A ", "Mr.".$_POST['field'], $c);
fclose($f);
$f=fopen($n,'w');
fwrite($f,$final);
fclose($f);
?>
Related
i want run php code inside str_replace();
examples:
data example is here:
"file":"https://example.com/hls2/02/00008/,kioaker9jwxw_n,.urlset/master.m3u8%22,%22id%22:%22kioaker9jwxw"}
My php code example:
$replace1 = str_replace('"file":"', '"file":"https://otherdomain.com/palx/test/?v=\<?php echo base64_encode("', $data);
$replace2 = str_replace('m3u8', 'm3u8")?\>', $replace1);
Edit;::::
i want change "example.com/m3u8" to other.com/ex.php?v=example.com/m3u8 but base64 encoded from data.
data contains one more links.
And contains tokens so changing file to file.
i can't touch /hls2/02/.... because changing file to file, this is auto bot.
In PHP there is fopen('r+') for Read/Write. (start at beginning of the file)
There is also fopen('a+') for Read/Write. (append to the end of the file)
I made a simple $_POST['data'] form that writes whatever is written in the form to a file. My problem is that if I write a few things, for example.
'Red '
'Green '
'Blue '
If I append this data, it will show up in the file in the order I write in.
But, I need to write to the beginning of the file, so that it's in reverse order.
'Blue '
'Green '
'Red '
$file_data = 'What to write in the file';
$file_data .= file_get_contents('testz.php');
file_put_contents('testz.php', $file_data);
This will write whatever is in the $file_data variable to the file and then write the data that was originally in the file afterwards.
Thus causing it to show up before everything else that was originally in the file. This works fine.
The problem starts when I want to include a line of code at the beginning of the file.
Specifically I want '!DOCTYPE html html head' to always be at the
top of the document. I could of course put that in the file data like:
file_put_contents('testz.php', $headerinformation.$file_data);
But the problem with that is gonna be everytime I submit the POST, It's gonna be repeating
<!DOCTYPE html><html><head>Blue
<!DOCTYPE html><html><head>Green
<!DOCTYPE html><html><head>Red
What I want is
<!DOCTYPE html><html><head>
Blue
Green
Red
So, then I can keep writing more information to the file via the POST form WITHOUT REPLACING THE FIRST LINE.
<!DOCTYPE html><html><head>
I know this is gonna be kind of a bad question, what what exactly can I do to achieve this?
First of all, you should write the first line at the time you create the file, e.g.:
$first_line = "<!DOCTYPE html><html><head>" . PHP_EOL;
file_put_contents("somefile.txt", $first_line);
And when you want to insert new content you could load your file into an array with file() and insert the content at index 1 of the array (which is the second line in the file) by using array_splice():
$lines = file("somefile.txt");
array_splice($lines, 1, 0, "new content");
Use implode() to join the array elements to a string and finally write to the file:
$file_content = implode(PHP_EOL, $lines);
file_put_contents("somefile.txt", $file_content);
Note that this approach will lead to memory issues when dealing with large files.
Why don't you check if file is already with some data in it, for eg.:
$data_to_write = file_get_contents('testz.php');
$header_data = '<!DOCTYPE html><html><head>';
$file_data .= (empty($data_to_write))?$header_data.$data_to_write:$data_to_write;
file_put_contents('testz.php', $file_data);
You can only add to the end of a file. You can write in the middle of it but only writing over existing data. So in order to add lines of text always as the second line you should load the data, add the line in the right place, then write the file again.
But I think what you are doing is a very weird thing to do, and most likely avoidable. You are writing an HTML file which makes me think you are going to display it at some point, in which case you should separate data handling and presentation.
For example you can treat the file as a data file where you just append lines without a header, then when you want to serve the data as html you output a header first and then the lines in reverse order.
I ended up doing it this way,
<?php
$f=fopen('WriteFile.php', 'r+');
$W='<!DOCTYPE html>'.file_get_contents('WriteFile.php');
$S='New Data';
fwrite($f, "$W");
fgets($f);
fseek($f,15);
fwrite($f, "$S");
?>
I want to create a registration system on my site where only limited users will be able to create their account. I want to use a .txt file for storing usernames and passwords.
I have the following code so far :
$uname=$_POST['usr'];
$pass=$_POST['pwd'];
if(empty($_POST["ok"])){echo "Could not insert data!";}
else
{$file=fopen("user.txt","w");
echo fwrite($file,$uname);
fclose($file);}
This receives the user data from a form and puts it in user.txt file.
My problem is that when new data is inserted to txt file the old data get deleted.
I want to keep the data in txt file like
foo:12345~bar:1111
username and password are seprated by : and new user is seprated by ~ ,later I will use regex to get the data from txt file.
How can i correct my code to keep both new and old data?
You need to open file in append mode
http://php.net/manual/en/function.fopen.php
<?php
$uname = $_POST['usr'];
$pass = $_POST['pwd'];
if (empty($_POST["ok"])) {
echo "Could not insert data!";
} else {
$file = fopen("user.txt", "a");
$srt="foo:".$uname."~bar:".$pass;// create your string
echo fwrite($file, $srt);
fclose($file);
}
If we want to add on to a file we need to open it up in append mode.
So you need to change from write only mode to append mode.
$file=fopen("user.txt","a");
To answer your question: you have to explicitly pass $mode argument to fopen() function equals to 'a'.
However, it looks like a bad idea to use plain files for this task. Mainly because of concurent writes troubles.
This is really a bad choice: there are a lot of drawbacks for security, for read/write times, for concurrent requests and a lot more.
Using a database isn't difficult, so my suggestion is to use one.
Anyway, your question is asked yet here: php create or write/append in text file
Simple way to append to a file:
file_put_contents("C:/file.txt", "this is a text line" . PHP_EOL, FILE_APPEND | LOCK_EX);
I'm using the Redactor editor in a custom built CMS. Redactor has an option, phpTags, which when set to true allows PHP code to be entered and saved as part of the content.
The issue is that this PHP code is being seen as text, not PHP code, and is being escaped rather than being processed.
For example, if I enter this in the editor:
<?php echo date('Y'); ?>
Instead of the year being displayed, the code is commented out in the page's markup, like so:
<!--?php echo date('Y'); ?-->
How can I prevent this from happening? To make sure the PHP code is processed/interpreted as such by the server?
I should probably mention that there are a lot of people using this CMS, so there's no way to know what PHP code may be added in advance.
Perhaps
<!-- <?php echo date('Y') ?> -->
You can't change PHP's opening/closing tags like you are, not without a recompile of PHP. If you want to hide php's output, then surround the entire php code block with html comment tags.
PHP won't care about the html comments. It couldn't care at all what it's embedded in. You could stuff a PHP code block into the middle of a .jpg file and it'd still execute, as long as the webserver's configured to run .jpg files through the PHP interpreter.
To fix this issue I took the content I was previously just displaying via echo, and saved it to a temporary file.
Then I turned on output buffering, included that temporary file in the PHP script, and grabbed its contents via ob_get_contents().
This allowed me to display the content with all the PHP within having been parsed. Here's the code for reference:
// Create path to temporary file
$tmpPath = '/temp.php';
// Set file variable to null for error checking
$tmpFile = NULL;
// Try creating the temporary file
if ( $tmpFile = fopen($tmpPath, 'w') ) {
if ( fwrite($tmpFile, $postContent) === FALSE ) {
// Do something if the file can't be written to
} else {
// Close file
fclose($tmpFile);
}
}
// Start output buffereing
ob_start();
// Include the temporary file created above
include $tmpPath;
// Save buffered contents to a variable
$content = ob_get_contents();
// End output buffering
ob_end_clean();
// Display content
echo $content;
I appreciate the various comments to my question, as it helped prod me in the right direction to getting this figured out.
I'm trying to create a simple one page php website that does the following.
Displays data from a txt file and then converts it into links.
(A line from the text file would be so if the line is "TextEdit: 1.9" ApplicationName = TextEdit)
So basically I need to add the url strings together with the application names in the desired locations and then redirect to that page. Hopefully ending up with something like:
Link[urlportion+appname+urlportion]
I am getting my data from a txt file that is formatted in the format of:
TextEdit: 1.9
AppleScript Editor: 2.6
Arduino: 1.0.5
TextWrangler: 4.5.3
etc.
I can display the code in my page using:
<?php
foreach(glob("log.txt") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode("<br>",$contents);
echo $string;
echo "<br></br>";
}
?>
That works beautifully my question is how do I separate the txt file into pieces so I can concatenate it with my url and display it.
If it's simple you can just use a text file and delimit your strings. You're going to need a server-side technology to do this - PHP, ASP.NET, etc. You haven't posted what your server-side technology is, but this can't be done without it.