Results 1 to 4 of 4

Thread: PHP script, just to read the first line.

  1. #1
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP script, just to read the first line.

    I was wondering, is there a way to open a file, such as a .txt file, then only read the first line, then close the file again? Thanks in advance.

    PS. Also, is there a way to write only to the first line, without wiping the whole .txt file? Thanks in advance again

    Please help with both of the above.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Reading the first line:
    PHP Code:
    $content file('mytextfilename.txt');
    //First line: $content[0];
    echo $content[0]; 
    For writing the first line, you can always overwrite the file by using:

    PHP Code:
    $fp fopen('mytextfilename.txt','w');
    fwrite($fp,'newcontent');
    fclose($fp); 
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reading first line part, but for the write part, I need it to write to the first line only. I already have it overwriting, but it's not what I need.

    Thanks!

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    $f file_get_contents($pathandname);
    $f explode("\n",$f,2);
    $f[0] = trim($f[0]);
    echo 
    $f[0]; //old stuff
    $f[1] = trim($f[1]);
    $file fopen($f,'w');
    fwrite($file,$newstuff."\n".$f[1]); //\n or \r\n on windows
    fclose($file); 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •