Results 1 to 3 of 3

Thread: How to edit text file without using a helping file using PHP

  1. #1
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to edit text file without using a helping file using PHP

    hey , I need a function which can take values from text boxes and save every value to the text file in certain place .
    I did this but I used an external text file , I took what I need from the original file then put the new values nect to them in the helping file and then I copied it content to my original file .

    my file :
    Code:
    Sally = 10 
    Sami = 50
    I want it to be like this :
    Code:
    Sally = 1230
    Sami = 23450
    here's my old code :
    Code:
    if(file_exists($trID_Log_File) && filesize($trID_Log_File) > 0)  {    
      $h = fopen($trID_Log_File, "r");
      $contents = fread($h, filesize($trID_Log_File));
      fclose($h);
      $out_h = fopen("output filename", "w");    
      if(!stristr($contents, "TrID is best suited to analyze binary files!")) {
        $lines = explode("\n", $contents);
        foreach($lines as $line) {
          if(strlen($line) > 5) {
            $line_arr=explode("=",$line);      
            if ($line_arr[0]=='Sally') {
              $line_arr[1]="10"; // The New Value 
            }
            fwrite($out_h, implode("=", $line_arr)."\n"); 
          }
        }
      }
    }
    so I'm writing to "output.txt" and then coping it back to my file , How can I do that ?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    That function looks like it's doing more than just writing to a file.

    Anyways, when writing to a file you always have the option of either appending to it or of overwriting it. It sounds like you want to overwrite the existing file. And unless you're working with PHP < version 5, it's a lot easier to do these sorts of things with:

    file_ get_ contents - gets the contents of the file as a string

    and:

    file_ put_ contents - writes a string variable to the file, optionally appending or overwriting.

    There's also just:

    file

    If you want to get the contents of the file as an array.

    See:

    http://php.net/manual/en/function.file.php
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    why are you checking this, exactly?
    PHP Code:
    if(!stristr($contents"TrID is best suited to analyze binary files!")) 
    and why do you assume there in an = in the string just because there's more than 5 characters?
    PHP Code:
          if(strlen($line) > 5) {
            
    $line_arr=explode("=",$line); 
    And do you want to deal only with "Sally"? Or might you have other name=>value pairs also? How many?

    Do you need to validate the names (i.e., allow only certain names, or allow any name submitted)?

    Do you need to validate the numbers, or just save them?

    Do you have to keep the name = number format, or are you willing to use something else that might be simpler?

Similar Threads

  1. Replies: 14
    Last Post: 07-15-2010, 05:53 PM
  2. Want to edit a php file from my php site
    By borris83 in forum PHP
    Replies: 3
    Last Post: 04-16-2009, 11:06 AM
  3. How To Edit A .dll File
    By tomyknoker in forum ASP
    Replies: 16
    Last Post: 04-16-2007, 02:54 AM
  4. edit upload file
    By pavmoxo in forum PHP
    Replies: 0
    Last Post: 04-18-2006, 04:29 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •