No announcement yet.

Dealing with text files in PHP

  • Filter
  • Time
  • Show
Clear All
new posts

  • Dealing with text files in PHP

    okay what I need to do is open a text file (not database!) and either store it into an array element per line or go through the file one line at a time so its like

    open filename
    for every line in file
    echo "Stuff";
    echo substring of the line;
    echo "more stuff";
    end for
    close filename

    Can anyone tell me the php syntax to do that?

    Motorsport Forums

  • #2

    heya mark,

    if you just want to read and display the file try this in a test php file..


    This will just read and display it..
    The Cannabis Edge


    • #3
      Thanks but what I need to do is a bit more complicated than that.

      What I have is a text file with a list of stuff - links as it happens.

      I need to go through it line by line and for each line add something to the start and something to the end, and actually only use part of the line (substr would work I guess).

      So I need access to the lines on an individual basis rather than just printing out the entire file.
      Motorsport Forums


      • #4

        Ok.. I would need a bit more info as to the structure of the lines.. Maybe post 3 or 4 lines of this file, what you wanted added at begining and end and what you need from each line.. I should be able to give a better example then.. hehe I think..

        The Cannabis Edge


        • #5
          ok it's something like

          <a href="">Link1</a>
          <a href="">Link2</a>

          Actually the line format needs to be irrelevant I need a general purpose routine which will let me read a line at a time, manipulate it in some as yet undefined way then let me print it on the screen, then go around and do the same again for the next one until EOF.
          Motorsport Forums


          • #6
            This is a routine which reads a text file a line at a time - I did it to reply to another forum question ( - this reads password info and checks user/password pairs

            but the basics of the file reading are all there,-

            "Hi muppet - you will have to lever this into your code - Here we fetch the text line by line (taken straight from the manual) as it is more efficient, we break the loop when a match is found.
            $fd = fopen("passtest.txt", "r");
            while (!feof($fd)) {
            $buffer = fgets($fd, 4096);//reading 1 lina at a atime//
            $split=explode(",",$buffer);//sticking the line in $buffer//
            if($split[0]==$PHP_AUTH_USER && chop($split[1])==$PHP_AUTH_PW){$gotit=1;break;}
            if($gotit==1){echo " username $user and password $pass are valid";}else {echo " username $user and password $pass are not valid";}

            I think the problem is solved by the chop() fuction which clears the whitespace from the password - without the chop() - the code was not working all the time - give it a try - I was using a textfile as below.


            The rest of your code is fine and would Authenticate once only as it is."

            Simon Wheeler.


            • #7
              The file() function will read a file into an array with each line being a new element. Thats how I made my text-file-based news script, and its working pretty well.


              • #8
                file() command!!

                heya mark,
                i've actually been doing this quite a lot lately.

                assuming that your txt file is full of links:


                you can use the following:

                $fpcontents = file('filepath');
                while ( list($line_num, $line) = each($fpcontents)) {
                echo "<a href='$line'>$line</a><br>\n";


                widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.