fread() function in PHP

WBOY
Release: 2023-09-08 06:00:01
forward
1779 people have browsed it

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)
Copy after login

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.
Copy after login
Copy after login

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);
?>
Copy after login

Output

Cricket
Copy after login

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);
?>
Copy after login

Output

Cricket and Football are popular sports.
Copy after login
Copy after login

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

Related labels:
source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template