Jump to content

Reading files in PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
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()

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


<?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:


<?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”)


<?php

$data = file(“data.txt”);

?>


Now the forwhile loop which will go through our $data array and list each one.


<?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 :D ;)

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You could also use file_get_contents to read the contents into a string as well.

+rep

#3
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
I did use the file() function, down the bottom of the tutorial. :D

Edit: On ya Jordan, ya bloody skim reader and then editing your posts :P

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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!

#5
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
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.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Very nice +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
reddem0n

reddem0n

    Newbie

  • Members
  • PipPip
  • 11 posts
thanks much, copied this one down.

#8
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
nice tut +rep

#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Very good. Very quick and useful. +rep :)

#10
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
+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"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#11
DiscoStu

DiscoStu

    Newbie

  • Members
  • Pip
  • 7 posts
This is cool (+rep), but can I make one suggestion?

If you're not absolutely sure that your text file is perfect, you may find that there are blank lines that you don't want to echo out, the following checks that the line actually has some data to output:

<?php
$data = file(“data.txt”);
foreach ($data as $value)
{
if($value) {
echo “$value<br>”
}
}
?>


#12
curiousplayer2003

curiousplayer2003

    Newbie

  • Members
  • Pip
  • 3 posts
Check this one
» Reading file using php Tutorials, Scripts, Technology and Interview Tips