Dealing with text files in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Hewitt
    Senior Member
    • Apr 2000
    • 1195
    • 4.1.x

    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?

    Cheers
    Motorsport Forums
  • Herb
    Senior Member
    • Apr 2000
    • 106

    #2
    Hmm..

    heya mark,

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

    readfile("path/to/file/filename.ext");

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

    Comment

    • Mark Hewitt
      Senior Member
      • Apr 2000
      • 1195
      • 4.1.x

      #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

      Comment

      • Herb
        Senior Member
        • Apr 2000
        • 106

        #4
        Oh

        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

        Comment

        • Mark Hewitt
          Senior Member
          • Apr 2000
          • 1195
          • 4.1.x

          #5
          ok it's something like

          <a href="www.com">Link1</a>
          <a href="www2.com">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

          Comment

          • firepages
            New Member
            • Jun 2000
            • 3

            #6
            This is a routine which reads a text file a line at a time - I did it to reply to another forum question (www.devshed.com) - 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.
            <?php
            $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;}
            }
            fclose($fd);
            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.

            1,mary
            2,john
            3,bill
            4,arthur
            5,elsie

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


            Simon Wheeler.

            Comment

            • Stallion
              Senior Member
              • Apr 2000
              • 704

              #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.

              Comment

              • bonzo
                New Member
                • Jul 2000
                • 2

                #8
                file() command!!

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

                assuming that your txt file is full of links:



                http://domain3.com/ etc...

                you can use the following:

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

                Comment

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