+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Reading files in PHP

  1. #1
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Reading files in PHP

    Reading files in PHP

    You may have a file hosted on your server that you want to read information from, in this quick PHP tutorial, I’ll show you an easy to understand way to do so.

    Firstly, have a file, for instance, data.txt, with some sample data in it, uploaded to the server.

    Some quick sample data:
    hello@example.com
    john@example.com
    david@example.com
    php@example.com

    Before you can read data in from the file, you will need to open the file using fopen()
    Code:
    <?php
    $file = “data.txt”;
    $fp = fopen($file, “r”);
    fclose($fp);
    ?>
    When done, you also need to close the file, with fclose().

    Next, we want to actually read the data in, in order to do this we’ll use the fgets() function. It takes 2 variables, the first, the pointer to our open file ($fp) and the second the amount of bytes we want to read in. Every character will be a byte. However one more thing to note, fgets only goes to the end of the first line. We will also implement a while loop to make it go to the end of the file – with the help of the File End of File function.

    Code:
    <?php
    $file = “data.txt”;
    $fp = fopen($file, “r”);
    while (!feof($fp)) {
    $data = fgets($fp, 1024);
    }
    fclose($fp);
    ?>
    Almost there, we just need to make it echo out our text:

    Code:
    <?php
    $file = “data.txt”;
    $fp = fopen($file, “r”);
    while (!feof($fp)) {
    $data = fgets($fp, 1024);
    echo “$data<br>”;
    }
    fclose($fp);
    ?>
    We add the HTML <br> tag, so that a new line begins where it is supposed to.

    There is another way to do it, using a function called file() and a foreach loop.

    First, we’ll set the variable $data to file(“data.txt”)

    Code:
    <?php
    $data = file(“data.txt”);
    ?>
    Now the forwhile loop which will go through our $data array and list each one.

    Code:
    <?php
    $data = file(“data.txt”);
    foreach ($data as $value)
    {
    echo “$value<br>”
    }
    ?>
    Its that simple! The second one is a lot easier to work with in my opinion.
    I hope my tutorial has helped and if it has don’t forget to +rep me
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Reading files in PHP

    You could also use file_get_contents to read the contents into a string as well.

    +rep

  4. #3
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: Reading files in PHP

    I did use the file() function, down the bottom of the tutorial.

    Edit: On ya Jordan, ya bloody skim reader and then editing your posts
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Reading files in PHP

    I always used file_get_contents to put it into a var, I will probably start using file() now! I never liked using fopen() or any of those f functions to read data from another file, I really only used those for editing files.

    +Rep!

  6. #5
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Reading files in PHP

    Nice tutorial. I'd not seen the file() method before, very handy.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Reading files in PHP

    Very nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    reddem0n is offline Newbie
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Re: Reading files in PHP

    thanks much, copied this one down.

  9. #8
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    Re: Reading files in PHP

    nice tut +rep

  10. #9
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Reading files in PHP

    Very good. Very quick and useful. +rep

  11. #10
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Reading files in PHP

    +rep
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# Tutorial: Reading and Writing XML Files
    By rueleonheart in forum CSharp Tutorials
    Replies: 3
    Last Post: 07-19-2011, 12:02 PM
  2. Beginner Reading INI Files in Managed Code
    By sam_coder in forum CSharp Tutorials
    Replies: 1
    Last Post: 02-04-2011, 01:25 AM
  3. Reading PDF files from C++
    By SankaSL in forum C and C++
    Replies: 2
    Last Post: 09-27-2010, 02:48 AM
  4. Reading and Writing Files in C
    By Guest in forum C Tutorials
    Replies: 39
    Last Post: 06-20-2010, 03:08 PM
  5. Visual Studio 2008: C# Reading Files
    By Jordan in forum CSharp Tutorials
    Replies: 8
    Last Post: 06-20-2008, 08:54 AM

Tags for this Thread

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