Home  >  Article  >  Backend Development  >  How to read file contents using fread() function in PHP

How to read file contents using fread() function in PHP

不言
不言Original
2019-02-22 09:50:233502browse

The fread() function in How to read file contents using fread() function in PHP is used to read content from How to read file contents using fread() function in PHP files. In this article, we will take a look at how to use the fread() function in How to read file contents using fread() function in PHP to read the content of the file. Let’s take a look at the specific content.

How to read file contents using fread() function in PHP

The basic syntax of the fread() function is as follows

fgets(file, length)

file: Specify the file to read the content

length: Optional parameter, specifying the number of bytes to read from the file.

Let’s look at two examples below

Example: The first 20 bytes will be read from the file.

<?How to read file contents using fread() function in PHP
  $fn = fopen("myfile.txt","r");
  $result = fread($fn, 20);
  echo $result;
  fclose($fn);
?>

Example 2: Read all content from file, here we use filesize() function to calculate the size of file and pass it as parameter to length.

<?How to read file contents using fread() function in PHP
  $fn = fopen("myfile.txt","r");
  $result = fread($fn, filesize($fn));
  echo $result;
  fclose($fn);
?>

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to read file contents using fread() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn