Home >Backend Development >PHP Tutorial >fread() function in PHP

fread() function in PHP

WBOY
WBOYforward
2023-09-07 23:57:041819browse

fread() function in PHP

fread() function reads data from an open file. The fread() function stops at the end of the file or when it reaches the specified length. Returns the read string on success. Returns FALSE on failure.

Syntax

fread(file_pointer, length)

Parameters

  • file_pointer − File system pointer resource created using fopen(). Required.

  • #length − The maximum number of bytes to read. Required.

Return value

If successful, the fread() function returns the read string. On failure, returns FALSE.

Suppose we have a file called "one.txt" that contains the following lines.

Cricket and Football are popular sports.

The following is an example that reads 7 bytes from a file.

Example

<?php
   $file_pointer = fopen("one.txt", "r");
   // fread() function
   echo fread($file_pointer, "7");
   fclose($file_pointer);
?>

Output

Cricket

Let’s look at an Another example of all bytes in the file "one.txt".

Example

<?php
   $file_pointer = fopen("one.txt", "r");
   // fread() function
   echo fread($file_pointer, filesize("one.txt"));
   fclose($file_pointer);
?>

Output

Cricket and Football are popular sports.

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

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete