PHP: Change specific line of txt File with ID - php

im searching for almost 2 hours for an solution for my problem.
i have a text file with a few lines:
Title1|||Content1
Title2|||Content2
Title3|||Content3
But now i want to change 2 specific line, for example Title2||Content2
i will send the id via url. so i know which line, but i wan't search via title or content which line php should change.
i have found this code:
$daten = file('../news.txt');
$fp = fopen('../news.txt', 'w');
foreach ($daten as $zeile){
$felder = explode('|||', $zeile);
if (!strcmp($felder[0], 'auto3')){
$felder[1] = 'xx';
$zeile = implode('-', $felder);
}
fwrite($fp, $zeile);
}
fclose($fp);
But how to change for expample
Title2|||Title3
of
$zeile[1]
which i get via
edit.php?id=1 ??

Sorry if I haven't understood your question correctly, don't kill me if my answer is wrong. In your case $daten is an array that contains the lines of the file "news.txt". $_GET['id'] will provide the offset that is required to edit the array.
$daten = file('../news.txt');
$fp = fopen('../news.txt', 'w');
$zeile = explode('|||', $daten[intval($_GET['id'])]);
$zeile[0] = 'ASD';//change Title1
$zeile[1] = 'FGH'."\r\n";//change Content1
$daten[intval($_GET['id'])] = implode('|||', $zeile);
fwrite($fp, implode('', $daten));

Related

PHP How to get the "file_put_contents" to work?

Im very new to programming and this is the first time i use PHP.
Im working on a webpage to switch 433mhz relays and show their current state.
But to know which switch is on or off you need to remember it when the button is clicked. So i thought of a States.txt file and update a line from 0 to 1. (or from 1 to 0)
I can already read a specific line, but when i try to write on a specific rule using "file_put_contents" it reads the line, and writes to a new file called like the rule.
$file = "States.txt";
$lines = file($file);
$btn1 = $lines[0];
$btn2 = $lines[1];
$btn3 = $lines[2];
$btn4 = $lines[3];
$btn5 = $lines[4];
$btn6 = $lines[5];
//file_put_contents($file, "test123");
file_put_contents( $lines[2] , "test123");
It should overwrite the specified rule, but instead it reads the line, and creates a new file called like the content of that line.
$file = "States.txt";
$lines = file($file);
$lines[2] = 'NEW VALUE' . PHP_EOL;
file_put_contents($file, $lines);

Sending multiple codes to an email depending on the quantity [PHP]

I am trying to create a script where I can sell my key codes/licence codes to people. Right now I have figured a way for the php script to read off a text file and output it then delete the code/key in the text file.
<?php
$content = file('test.txt');
echo $content[0];
$file = "test.txt";
$line = 0;
$array = file($file);
unset($array[$line]);
$fp = fopen($file, 'w+');
foreach($array as $line)
fwrite($fp,$line);
fclose($fp);
?>
My question is that is there a way to integrate PayPal with this and they can pick multiple quantities. So for example if they picked 5 codes in the checkout a php script would email them 5 codes and delete them from the list.

read the first line of the txt file then delete that line with php

I have a huge txt file that have 475254 lines and with php I want to read the first line of the my txt file and save it into the Variable and then when I save it the php delete that line.
my txt file is about 2.3 MB is it possible to do this?
yes it is /.................................
OK less trolling..
You want fopen and fgets will grab a line. REF : fgets Manual PHP
$file = "file.txt"
$f = fopen($file, 'r');
$line = fgets($f);
fclose($f); // You close because you only want the first one.
There are so many examples how to do this i feel embarrassed answering. You should show some of what you have tried first!
Now you want to remove it: use file_get_contents REF : PHP file_get_contents
//Get your file contents
$newDoc = file_get_contents($file, true);
$newFileContents = substr( $line, strpos($newDoc, "\n")+1 );
//then you want to save it
file_put_contents($newFileContents, $file);
I might be wrong but you get the idea!~ ;)
Process :
Get Contents of file
Get First Line
Replace content of all file with your First Line as New Line
Save File
Im sure there is a more efficient way to do this, im just winging!
NOTE: You may need to configure your php.ini to work with larger files!
yes, Mark is probably too lazy to even try bulid a code, but i have already a working code so.. copypasta
$file = "mydata.txt";
$f = fopen($file, 'r');
$line = fgets($f);
fclose($f);
//do smth
$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));
//sleep(1);

Explode() is giving server error, page can't load

<?php
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
print_r ($theData);
fclose($fh)
?>
This is my current code, which has successfully read my file and printed the data to the screen. However now when I try to explode the data I just get a sever error and the page doesn't load at all, the only error message I get is page may be down for maintenance or configured incorrectly and I don't understand why it isn't working.
I am trying to put
$my_array = explode("/n", $theData);
after the data has been read, and before it is printed, but every time I add it the page gives up, but when I take it out the page loads again fine.
I need to be able to put in a foreach loop to explode the data and print it out one line at a time (it's an email directory) but I don't understand why it's not working.
$myFile = "file.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$assoc_array = array()
$my_array = explode("\n", $theData);
foreach($my_array as $line)
{
$tmp = explode(" ", $line);
$assoc_array[$tmp[0]] = $tmp[1];
}
fclose($fh)
$mail = $assoc_array;
I have tried this code, which I found while doing the original research for how to read from .txt file to array, but it still throws up the server error problem.
Could someone explain where I'm going wrong?
In the end the code I've used is:
<?php
// Open the file
$filename = 'pvemail.txt';
$fp = fopen($filename, 'r');
// Add each line to an array
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
print_r ($array);
?>
I've managed to read the data and print each line out into an array, now all I need to do is make it look nice! Thanks a lot for your help guys!
Initial problems
Looks like you have a few missing semicolons, unless you typed the reference code by hand and your actual code is correct.
$assoc_array = array()
fclose($fh)
offset 1 does not exist
$tmp[1] does not exist, which means some $tmp either had no values or only one value. It is most likely the case that one of the lines is a single word without a space or completely empty.

How to overwrite a particular line in flat file?

My text file contains:
a
b
c
d
e
I can't figure out how to amend my code so that I can overwrite line 3 ONLY (ie replacing "c") with whatever I type into the input box 'data'. My code is as follows, currently the contents of the input box 'data' replaces my file entirely:
$data = $_POST['data'];
$file = "data.txt";
$fp = fopen($file, "w") or die("Couldn't open $file for writing");
fwrite($fp, $data) or die("Couldn't write values to file");
fclose($fp);
I have it working the other way around, ie the code below reads line 3 ONLY into the text box when the page first loads:
$file = "data.txt";
$lines = file( $file );
echo stripslashes($lines[2]);
Can anybody advise the code I need to use to achieve this?
The only way is to read the whole file, change the 3rd line, then write it all back out. Basically, like so:
$lines = file($file);
$lines[2] = $_POST['data'];
file_put_contents($file, implode("\n", $lines));
Btw, your reading code does not "ONLY" read line 3 - it reads all lines as per file() and then you only use line 3.

Categories