Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I write to a certain part of a text file using PHP??

    I wanna do sumthing where I can write wether a person is on or not. If they are not logged on the beginning of the text file that is being read will say OFF, and vise versa. But that wont be the only info in the text file, but that will be the first line. What could I do to make the PHP change only the first line????????????????????? Any help would be greatly appreciated!

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I had a similar problem with this, and this is how I went about dealing with it.

    You know the first line of the text file is the attribute of what the user is doing (off or on)

    when you read your file:

    PHP Code:

    $id 
    'nameOfFile.txt';
    $file file($id); //this is to make all the lines into an array
    $handle fopen($id"r+");
    flock($handle2); //lock the file from any other changes but the one this script makes
    $length count($file); //what is the length of the array
    $file[0] = "ON"//or "OFF" either way...

    fclose(fopen($files,'w')); //open, empty and close a file

    for ($i 0$i $length$i++)
    {
         echo 
    $file[$i].'<br />';
         
    fputs($handle$file[$i]);
    }
    //end for

    fputs($handle$comment);
    flock($handle3); //unlock the file, so there is only one change at a time ie. no information will be lost
    fclose($handle); //close the file 
    Note: that this script hasn't been tested, but this is the gist of what you have to do. The comments should help you figure out the rest if the code doesn't entirely work. Good luck.

    Duniyadnd


  •  

    Posting Permissions

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