Php view text file

PHP Read File

Summary: in this tutorial, you’ll learn how to read a file using the various built-in PHP functions.

To read the contents from a file, you follow these steps:

  • Open the file for reading using the fopen() function.
  • Read the contents from the file using the fread() function.
  • Close the file using the fclose() function.

Here’s the syntax of the fread() function:

fread ( resource $stream , int $length ) : string|falseCode language: PHP (php)

The fread() function has two parameters:

  • The $stream is a file system pointer resource, which is typically the result of the fopen() function.
  • The $length specifies the maximum number of bytes to read. If you want to read the entire file, you can pass the file size to the $length parameter.

The fread() function returns the file contents or false if it fails to read.

Читайте также:  Php script cron cpanel

The fread() function stops reading the file once the $length number of bytes has been read or the end of file (EOF) has been reached.

To check if the file pointer is at end of file, you can pass it to the feof() function:

feof ( resource $stream ) : bool Code language: PHP (php)

The feof() function returns true if the $stream is at the EOF or an error occurs. Otherwise, it returns false .

To read a file line by line, you use the fgets() function:

fgets ( resource $handle , int $length = ? ) : string|falseCode language: PHP (php)

Like the fread() function, the fgets() function accepts a file system pointer resource and up to a number of bytes to read. If you omit the $length argument, the fread() function will read the entire line.

PHP read file examples

Let’s take some examples of how to read a file.

1) Read the entire file into a string

Suppose that you have a file named population.txt located at public directory with the following contents:

1 New York New York 8,253,213 2 Los Angeles California 3,970,219 3 Chicago Illinois 2,677,643 4 Houston Texas 2,316,120 5 Phoenix Arizona 1,708,127 6 Philadelphia Pennsylvania 1,578,487 7 San Antonio Texas 1,567,118 8 San Diego California 1,422,420 9 Dallas Texas 1,343,266 10 San Jose California 1,013,616 Code language: plaintext (plaintext)

The following example uses the fread() function to read the contents of the entire population.txt file into a string and shows it on the webpage:

 $filename = './public/population.txt'; $f = fopen($filename, 'r'); if ($f) < $contents = fread($f, filesize($filename)); fclose($f); echo nl2br($contents); > Code language: HTML, XML (xml)

First, open the population.txt file using the fopen() function:

$f = fopen($filename, 'r');Code language: PHP (php)

Second, read the contents of the entire file using the fread() function; use the filesize() function to get the size of the file:

$contents = fread($f, filesize($filename));Code language: PHP (php)

Third, show the contents of the file on a web page; use the nl2br() function to convert the newline characters to
tags.

echo nl2br($contents);Code language: PHP (php)

Finally, close the file using the fclose() function.

Note that the file_get_contents() function is a shortcut for opening a file, reading the whole file’s contents into a string, and close it.

2) Read some characters from a file

To read some characters from a file, you specify the number of bytes to read. The following example uses the fread() function to read up to 100 bytes from the population.txt file:

 $filename = './public/population.txt'; $f = fopen($filename, 'r'); if ($f) < $contents = fread($f, 100); fclose($f); echo nl2br($contents); >Code language: HTML, XML (xml)
1 New York New York 8,253,213 2 Los Angeles California 3,970,219 3 Chicago Illinois 2,677,64Code language: plaintext (plaintext)

3) Read a file line by line

The following example uses the fgets() funtion to read the population.txt file line by line:

 $filename = './public/population.txt'; $lines = []; $f = fopen($filename, 'r'); if (!$f) < return; > while (!feof($f)) < $lines[] = fgets($f); >print_r($lines); fclose($f);Code language: HTML, XML (xml)

Summary

  • Use the fread() function to read some or all contents from a file.
  • Use the fgets() function to read a line from a file.
  • Use the feof() function to test the end-of-file has been reached.
  • Use the filesize() function to get the size of the file.

Источник

PHP Read File

Summary: in this tutorial, you’ll learn how to read a file using the various built-in PHP functions.

To read the contents from a file, you follow these steps:

  • Open the file for reading using the fopen() function.
  • Read the contents from the file using the fread() function.
  • Close the file using the fclose() function.

Here’s the syntax of the fread() function:

fread ( resource $stream , int $length ) : string|falseCode language: PHP (php)

The fread() function has two parameters:

  • The $stream is a file system pointer resource, which is typically the result of the fopen() function.
  • The $length specifies the maximum number of bytes to read. If you want to read the entire file, you can pass the file size to the $length parameter.

The fread() function returns the file contents or false if it fails to read.

The fread() function stops reading the file once the $length number of bytes has been read or the end of file (EOF) has been reached.

To check if the file pointer is at end of file, you can pass it to the feof() function:

feof ( resource $stream ) : bool Code language: PHP (php)

The feof() function returns true if the $stream is at the EOF or an error occurs. Otherwise, it returns false .

To read a file line by line, you use the fgets() function:

fgets ( resource $handle , int $length = ? ) : string|falseCode language: PHP (php)

Like the fread() function, the fgets() function accepts a file system pointer resource and up to a number of bytes to read. If you omit the $length argument, the fread() function will read the entire line.

PHP read file examples

Let’s take some examples of how to read a file.

1) Read the entire file into a string

Suppose that you have a file named population.txt located at public directory with the following contents:

1 New York New York 8,253,213 2 Los Angeles California 3,970,219 3 Chicago Illinois 2,677,643 4 Houston Texas 2,316,120 5 Phoenix Arizona 1,708,127 6 Philadelphia Pennsylvania 1,578,487 7 San Antonio Texas 1,567,118 8 San Diego California 1,422,420 9 Dallas Texas 1,343,266 10 San Jose California 1,013,616 Code language: plaintext (plaintext)

The following example uses the fread() function to read the contents of the entire population.txt file into a string and shows it on the webpage:

 $filename = './public/population.txt'; $f = fopen($filename, 'r'); if ($f) < $contents = fread($f, filesize($filename)); fclose($f); echo nl2br($contents); > Code language: HTML, XML (xml)

First, open the population.txt file using the fopen() function:

$f = fopen($filename, 'r');Code language: PHP (php)

Second, read the contents of the entire file using the fread() function; use the filesize() function to get the size of the file:

$contents = fread($f, filesize($filename));Code language: PHP (php)

Third, show the contents of the file on a web page; use the nl2br() function to convert the newline characters to
tags.

echo nl2br($contents);Code language: PHP (php)

Finally, close the file using the fclose() function.

Note that the file_get_contents() function is a shortcut for opening a file, reading the whole file’s contents into a string, and close it.

2) Read some characters from a file

To read some characters from a file, you specify the number of bytes to read. The following example uses the fread() function to read up to 100 bytes from the population.txt file:

 $filename = './public/population.txt'; $f = fopen($filename, 'r'); if ($f) < $contents = fread($f, 100); fclose($f); echo nl2br($contents); >Code language: HTML, XML (xml)
1 New York New York 8,253,213 2 Los Angeles California 3,970,219 3 Chicago Illinois 2,677,64Code language: plaintext (plaintext)

3) Read a file line by line

The following example uses the fgets() funtion to read the population.txt file line by line:

 $filename = './public/population.txt'; $lines = []; $f = fopen($filename, 'r'); if (!$f) < return; > while (!feof($f)) < $lines[] = fgets($f); >print_r($lines); fclose($f);Code language: HTML, XML (xml)

Summary

  • Use the fread() function to read some or all contents from a file.
  • Use the fgets() function to read a line from a file.
  • Use the feof() function to test the end-of-file has been reached.
  • Use the filesize() function to get the size of the file.

Источник

6 Ways to Read Files In PHP – Into String, Array, And More!

Welcome to a tutorial on how to read files in PHP. Yep, that is right, how difficult can reading files in PHP be? You will be surprised… It is not as straightforward as some may think.

There are quite a number of ways to read files in PHP:

  1. Read file into a string – $contents = file_get_contents(«FILE»);
  2. Read file into an array – $array = file(«FILE»);
  3. Use cURL to fetch a file from a different server.
    • $curl = curl_init(«http://site.com/»);
    • curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    • $data = curl_exec($curl);
    • curl_close($curl);
  4. Open a file stream and read line-by-line.
    • $stream = fopen(«FILE», «r»);
    • while($line = fgets($stream))
    • Read and directly output – readfile(«FILE»);
    • Directly load a file into the script – include «FILE»; require «FILE»;

That is a quick overview of the common methods, but let us walk through some examples in this guide – Read on!

TLDR – QUICK SLIDES

6 Ways To Read Files In PHP

TABLE OF CONTENTS

READ FILES IN PHP

All right, let us now get into the various ways to read files in PHP.

1) READ FILE INTO A STRING

There is not much rocket science involved here, just take note that file_get_contents() can fetch contents from a file or URL. While this is straightforward, take extra care to not read massive files with this… You will run into an “out of memory” error, and possibly cause some issues on the server.

2) READ FILE INTO AN ARRAY

This is another simple one. But instead of a flat string, file() will read into an array instead, with each element being a different line. Take note again, do not attempt to read large files with this.

3) CURL FETCH

CURL stands for “client URL”, and in layman’s term “server-to-server calls”. Some of you guys may be thinking – What the heck, isn’t this a roundabout way of doing file_get_contents() ? Well no. CURL actually offers a lot of options and controls – It can even fetch files from an FTP server. Will leave the reference links below if you are interested.

4) READ FILE LINE-BY-LINE

 // (C) CLOSE FILE fclose($handle);

The above 3 methods will run into a memory problem when dealing with large files. So to deal with that problem, we can use fgets() and read line-by-line instead.

5) FILE STREAM

This next method is a little different from the rest. Instead of reading a file into strings and arrays, it directly outputs it. Very useful when it comes to forcing a download or transferring huge files.

6) INCLUDE OR REQUIRE

You have probably heard of include and require since day 1 of learning PHP – Yes, they can be used for any file type actually.

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

SUPPORT

600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

That’s all for this guide, and here are some extras and links that may be useful.

THE SUMMARY

YOUTUBE TUTORIAL

INFOGRAPHIC CHEAT SHEET

THE END

Thank you for reading, and we have come to the end of this short guide. I hope this has helped to solve your file reading yoga issues, and if you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!

Источник

Оцените статью