PHP Write and Read from 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);
 
 
 
 
 
$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;
    }

} 







<?php 
    $text = $_REQUEST['message'];
    $f = file_get_contents("all.txt"); 
    $f = explode(", ", $f); 

    function modFile($pos, $tothis, $inthis)
    { 
        foreach($inthis as $pos => $a){ 
        } 
        $newarr = implode("\r\n", $inthis); 
        $fh = fopen("example.txt", "w"); 
        fwrite($fh, $newarr); 
        fclose($fh); 
    } 

    modFile(4, '', $f); 
 
 
If you're looking to keep a specific/exact 
file structure, you might wanna consider (changing to) saving files as 
xml instead (that way you can use existing markup instead of inventing 
your own). If you do that, you also might be better off in the long run 
considering xml is very widely used