PHP File Handling: Reading File

Submitted by: 
Language: 
Visitors have accessed this post 1840 times.

In this tutorial I'm going to show how to read back information from files. So before we can read information from a file we need to use a function fopen just to open the file for reading. In this lesson, we’re going to use the “hello.txt” file that we created in the writing of file lesson.
To start in this lesson, we are going to create a new PHP file called "readfile.php". Then add the following code:

  1. <?php
  2. //specify the file we want same thing when we writing a file
  3. $testfile = 'hello.txt';
  4. //this code is similar to our writing of file
  5. if($fh = fopen($testfile, 'r')){// we are reading
  6.  
  7. $content = fread($fh, 5);//each character is equivalent to 1 byte
  8. //close the handle
  9. fclose($fh);
  10. }
  11.  
  12. //we will echo the output here
  13. echo $content;
  14. echo '<br/>';
  15. //this will take each new line and turn it into br tag so that HTML will show it properly
  16. echo nl2br($content);
  17. ?>

Output:

hello
hello

We have this result because we simply specify the number of characters we want to display, in our case we write five(5) character. But what if we want to read the entire file and you don’t know how many byte the file is. And add the following code for the second example.

  1. <?php
  2. //specify the file we want same thing when we writing a file
  3. $testfile = 'hello.txt';
  4. //this code is similar to our writing of file
  5. if($fh = fopen($testfile, 'r')){// we are reading
  6.  
  7. //the fread handle using the the filesize it reads
  8. //how many bytes are you or gets the total number of bytes
  9. $content = fread($fh, filesize($testfile));
  10. //close the handle
  11. fclose($fh);
  12. }
  13.  
  14. //we will echo the output here
  15. //this will take each new line and turn it into br tag so that HTML will show it properly
  16. echo nl2br($content);
  17. ?>

Output:

hello
world

But I have here another way using the shortcut for fopen/fread/fclose, and this is also a companion to shortcut  file_put_contents(filename, data). And here’s add the following code.

  1. <?php
  2. //specify the file we want same thing when we writing a file
  3. $testfile = 'hello.txt';
  4. //using the shortcut for fopen/fread/fclose
  5. $content = file_get_contents($testfile);
  6. echo nl2br($content);
  7. ?>

Output:

hello
world

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.