Home  >  Article  >  Backend Development  >  How to use php fread function

How to use php fread function

藏色散人
藏色散人Original
2019-05-28 15:12:492706browse

php The fread function is used to read files (safe for binary files). Its syntax is fread(file,length). The parameter file is required, which means that the file must be read and opened. length is required, which means that it must be read. The maximum number of bytes.

How to use php fread function

#How to use php fread function?

Definition and Usage

fread() function reads a file (safe to use with binary files).

Syntax

fread(file,length)

Parameters

file Required. Specifies that the file should be opened for reading.

length Required. Specifies the maximum number of bytes to read.

Description

fread() reads up to length bytes from the file pointer file. This function is called after reading up to length bytes, or when EOF is reached, or (for network streams) when a packet is available, or (after opening a user space stream) 8192 bytes have been read. Will stop reading the file, depending on which condition is encountered first.

Return the read string, or false if an error occurs.

Tips and comments

Tips: If you just want to read the contents of a file into a string, please use file_get_contents(), which has better performance than fread () is much better.

Example 1

Read 10 bytes from the file:

<?php
$file = fopen("test.txt","r");
fread($file,"10");
fclose($file);
?>

Example 2

Read the entire file:

<?php
$file = fopen("test.txt","r");
fread($file,filesize("test.txt"));
fclose($file);
?>

The above is the detailed content of How to use php fread function. 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