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  
Related Posts:
  • Php get_meta_tags-Extracts all meta tag content attributes get_meta_tags — Extracts all meta tag content attributes from a file and returns an array <?php// Assuming the above tags are at www.example.com$tags = get_meta_tags('htt… Read More
  • PHP Classes Classes do not use scope keywords, but you can prevent people from instantiating the class by makingthe __construct() method and the __clone() methods private or protected. The __construct()method is used to create the objec… Read More
  • Finding the Position of a Value in an Array Use array_search( ) . It returns the key of the found value. If the value is not in the array, it returns false:   $position = array_search($value, $array); if ($position !== false) { // the element in position&n… Read More
  • You want to extract part of a string, starting at a particular place in the string Use substr( ) to select your substrings: $substring = substr($string,$start,$length); $username = substr($_REQUEST['username'],0,8); … Read More
  • Reading DOC file in php   read PDF and DOC files using PHP Reading PDF Files   $content = shell_exec('/usr/local/bin/pdftotext '.$filename.' -');     Reading DOC Files  $content = shell_exec('/usr/local/bin/antiword '.$fi… Read More
  • Checking Variable Values and Types FUNCTION is_numeric() True if number or numeric stringctype_digit() True if all digits are numeric charactersis_bool() True if variable is a Booleanis_null() True if variable is NULLis_float() True if variable type is a fl o… Read More
  • Control Structures if else elseif/else if Alternative syntax for control structures while do-while for foreach break continue switch declare return require include require_ once include_ once goto … Read More
  • PHP Redirect - Redirect Script? PHP Redirect Function header('Location: destination.php'); exit();   ou need the Location: part so the browser knows what header it's  receiving. Also, don't forget to  do an exit() or die() right after… Read More
  • Extracting Substrings You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.  Extracting a substring with substr( ) &l… Read More
  • What is triggers? A trigger is a database object which is associated with particular database table. Triggers gets called automatically when particular event(INSERT, UPDATE, DELETE) occurs on table. … Read More
  • $_ENV and $_SERVER ? PHP sets several variables for you containing information about the server, the environment, and your visitor's request. These are stored in the superglobal arrays $_ENV and $_SERVER, but their availability depends on whe… Read More
  • magic_quotes_gpc, magic_quotes_runtime Magic quotes is the name of a PHP feature that automatically quotes inputdata, by using the addslashes() function. Historically, this was used so thatform data could be used directly in SQL queries without any security or qu… Read More
  • Setting Environment Variables Setting environment variables in your server configuration on a host-by-host basis allows you to configure virtual hosts differently. <?php putenv('ORACLE_SID=ORACLE'); // configure oci extension ?>   Adjusting… Read More
  • Turning an Array into a String convert it into a  formatted string. Use join( ): // make a comma delimited list $string = join(',', $array); Or loop yourself: $string = ''; foreach ($array as $key => $value) { $string .= ",$value"; } $str… Read More
  • Setting Default Values for Function Parameters Assign the default value to the parameters inside the function prototype: function wrap_html_tag($string, $tag = 'b') { return "<$tag>$string</$tag>"; }     The example in the Solution sets the… Read More