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()
When done, you also need to close the file, with fclose().Code:<?php $file = “data.txt”; $fp = fopen($file, “r”); fclose($fp); ?>
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.
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); } fclose($fp); ?>
We add the HTML <br> tag, so that a new line begins where it is supposed to.Code:<?php $file = “data.txt”; $fp = fopen($file, “r”); while (!feof($fp)) { $data = fgets($fp, 1024); echo “$data<br>”; } fclose($fp); ?>
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”)
Now the forwhile loop which will go through our $data array and list each one.Code:<?php $data = file(“data.txt”); ?>
Its that simple! The second one is a lot easier to work with in my opinion.Code:<?php $data = file(“data.txt”); foreach ($data as $value) { echo “$value<br>” } ?>
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!
You could also use file_get_contents to read the contents into a string as well.
+rep
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!
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!
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.
Very nice +rep
thanks much, copied this one down.
nice tut +rep
Very good. Very quick and useful. +rep![]()
+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"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks